Brandon's Code Project

I want to share my code to everybody.

Create a function that accepts 3 parameters and checks for equality between any two of them.

Your function should return True if 2 or more of the parameters are equal, and false is none of them are equal to any of the others.

Modify your function so that strings can be compared to integers if they are equivalent. For example, if the following values are passed to your function:

6,5,“5”

You should modify it so that it returns true instead of false.

Hint: there’s a built in Python function called “int” that will help you convert strings to Integers.

def function_num_comparison(x, y):
    if x >= y:
        print(x, "is greater than or equal to", y)
    elif y <= x:
        print(x, "is less than or equal to", x)
    elif x > y:
        print(x, "is greater than", y)
    elif y < x:
        print(y, "is less than", x)
    elif y > x:
        print(y, "is greater than", x)
    elif x < y:
        print(x, "is less than", y)
    else:
        print("Error")

print(function_num_comparison(3.5,8))

def function_rock_paper_scissors_comparison(rock_paper_scissors_first_user, rock_paper_scissors_second_user, rock_paper_scissors_third_user):
    if rock_paper_scissors_first_user == "rock" and rock_paper_scissors_second_user == "paper" and rock_paper_scissors_third_user == "scissors":
        if len(rock_paper_scissors_second_user*2) > len(rock_paper_scissors_first_user):
            print(rock_paper_scissors_second_user, "beats", rock_paper_scissors_first_user)
            print(True)
            print("___________________")
        if len(rock_paper_scissors_first_user*2) > len(rock_paper_scissors_third_user):
            print(rock_paper_scissors_first_user, "beats", rock_paper_scissors_third_user)
            print(True)
            print("_____________________")
        if len(rock_paper_scissors_third_user*2) > len(rock_paper_scissors_second_user):
            print(rock_paper_scissors_third_user, "beats", rock_paper_scissors_second_user)
            print(True)
            print("______________________")
            
    elif rock_paper_scissors_first_user == "paper" and rock_paper_scissors_second_user == "rock" and rock_paper_scissors_third_user == "scissors":
        if len(rock_paper_scissors_first_user*2) > len(rock_paper_scissors_second_user):
            print(rock_paper_scissors_first_user, "beats", rock_paper_scissors_second_user)
            print(True)
            print("___________________")
        if len(rock_paper_scissors_second_user*2) > len(rock_paper_scissors_third_user):
            print(rock_paper_scissors_second_user, "beats", rock_paper_scissors_third_user)
        if len(rock_paper_scissors_third_user*2) > len(rock_paper_scissors_first_user):
            print(rock_paper_scissors_third_user, "beats", rock_paper_scissors_first_user)
            print(True)
            print("___________________")
            
    elif rock_paper_scissors_first_user == "scissors" and rock_paper_scissors_second_user == "paper" and rock_paper_scissors_third_user == "rock":
        if len(rock_paper_scissors_first_user*2) > len(rock_paper_scissors_second_user):
            print(rock_paper_scissors_first_user, "beats", rock_paper_scissors_third_user)
            print(True)
            print("___________________")
        if len(rock_paper_scissors_third_user*2) > len(rock_paper_scissors_first_user):
            print(rock_paper_scissors_third_user, "beats", rock_paper_scissors_first_user)
        if len(rock_paper_scissors_second_user*2) > len(rock_paper_scissors_third_user):
            print(rock_paper_scissors_second_user, "beats", rock_paper_scissors_third_user)
            print(True)
            print("___________________")
            
    elif rock_paper_scissors_first_user == "paper" and rock_paper_scissors_second_user == "scissors" and rock_paper_scissors_third_user == "rock":
        if len(rock_paper_scissors_first_user*2) > len(rock_paper_scissors_third_user):
            print(rock_paper_scissors_first_user, "beats", rock_paper_scissors_third_user)
            print(True)
            print("___________________")
        if len(rock_paper_scissors_second_user*2) > len(rock_paper_scissors_first_user):
            print(rock_paper_scissors_second_user, "beats", rock_paper_scissors_first_user)
            print(True)
            print("___________________")
        if len(rock_paper_scissors_third_user*2) > len(rcok_paper_scissors_second_user):
            print(rock_paper_scissors_third_user, "beats", rock_paper_scissors_second_user)
            print(True)
            print("___________________")
        if len(rock_paper_scissors_third_user*2) > len(rock_paper_scissors_second_user):
            print(rock_paper_scissors_third_user, "beats", rock_paper_scissors_second_user)
            print(True)
            print("___________________")

print()
function_rock_paper_scissors_comparison("paper", "rock", "scissors")

def number_comparison_function(a, b, c):
    
    if a > b:
        print(a, "is greater than", c)
        print(True)
        print()
    elif a > b:
        print(a, "is not greater than", b)
        print(False)
        print()
        
    if b > a:
        print(b, "is greater than", a)
        print(True)
        print()
    elif b > a:
        print(b, "is not greater than", a)
        print(False)
        print()
        
    if c > a:
        print(c, "is greater than", a)
        print(True)
        print()
    elif c > a:
        print(c, "is not greater than", a)
        print(False)
        print()
        
    if c > b:
        print(c, "is greater than", b)
        print(True)
        print()
    elif c > int(b):
        print(c, "is not greater than", b)
        print(False)
        print()
        
    if a < b:
        print(a, "is less than", b)
        print(True)
        print()
    elif a < int(b):
        print(a, "is not less than", int(b))
        print(False)
        print()
        
    if float(b) < a:
        print(float(b), "is less than", a)
        print(True)
        print()
    elif float(b) < a:
        print(float(b), "is not less than", a)
        print(False)
        print()

    if c < a:
        print(c, "is less than", a)
        print(True)
        print()
    elif c < a:
        
        print(c, "is not less than", a)
        print(False)
        print()
    
    if c < int(b) and int(b) < c:
        print(c, "is less than", int(b) ,"and ", int(b), "is less than", c)
        print(True)
    elif c < b:
        print(c, "is not less than", b)
        print(False)
        print()
    
    if a == int(b):
        print(a, "is equal to", int(b))
        print(True)
    elif float(b) == a and float(a) == int(a):
        print(b, "is not equal to", a)
        print(False)
        print()

    if a == c:
        print(a, "is equal to", c)
        print(True)
        print()
    elif c == a:
        print(c, "is not equal to", a)
        print(False)
        print()

    if b == c:
        print(b, "is equal to", c)
        print(True)
        print()
    elif c == b and 2.912 == float(b):
        print(c, "is not equal to", float(b) ,"and", 2.912, " is not equal to", float(b))
        print(False)

number_comparison_function(1.582, 5.9242 , 3.2191392)
print()
1 Like

‘’’
Homework Assignment #8: Input and Output (I/O)

Details:

Create a note-taking program. When a user starts it up, it should prompt them for a filename.

If they enter a file name that doesn’t exist, it should prompt them to enter the text they want to write to the file.
After they enter the text, it should save the file and exit.

If they enter a file name that already exists, it should ask the user if they want:

A) Read the file

B) Delete the file and start over

C) Append the file

If the user wants to read the file it should simply show the contents of the file on the screen.
If the user wants to start over then the file should be deleted and another empty one made in its place.
If a user elects to append the file, then they should be able to enter more text, and that text should be added to the existing text in the file.
‘’’

from typing import TextIO
import csv
import os

def Read_The_File():
    Your_Name = str(input("Enter your name: "))
    Your_Name_File = open("Your Name", "w")
    with open("Your_Name_File.csv", "w") as Your_Name_:
        write_file_Your_Name = csv.writer(Your_Name_)
        Today_Date = str(input("Enter today's date: "))
        Today_Date_File = open("Today's Date", "w")
    with open("Today_Date_File.csv", "w") as today_date_file:
        write_file_Today_Date_File = csv.writer(today_date_file)
        Menu_Editor = "     Menu Editor:                                             "
        Menu_Editor_File = open("Menu Editor", "w")
    with open("Menu_Editor_File.csv", "w") as menu_editor_file:
        write_file_Menu_Editor_File = csv.writer(menu_editor_file)
        Note_Taking_Program = str(input("Enter your notes here: "))
        Note_Taking_Program_File = open("Note Taking Program", "w")
    with open("Note_Taking_Program_File.csv", "w") as note_taking_program_file:
        write_file_Note_Taking_Program_File = csv.writer(note_taking_program_file)
        user_input_1 = str(input("Enter (yes/no): Do you want to continue reading this file? "))
    if (user_input_1 == "yes"):
        print()
        print("Notes")
        print(Menu_Editor)
        print(Your_Name)
        print("---------------------------------------------------------------------")
        print("Enter your notes here: ")
        print(Note_Taking_Program)
        print()
    for user_input_1 in user_input_1:
        break

def Delete_File_and_Start_Over():
    user_input_2 = str(input("Enter (yes/no): Do you want to delete the file and start over? "))
    if user_input_2 == "yes" or user_input_2 == "Yes":
        number_of_times = int(input("Enter the number of times you want to put input variables and print file: "))
    for count in range(0, number_of_times):
        Your_Name = str(input("Enter your name: "))
        Your_Name_File = open("Your Name", "w")
        with open("Your_Name_File.csv", "w") as Your_Name_:
            write_file_Your_Name = csv.writer(Your_Name_)
            Today_Date = str(input("Enter today's date: "))
            Today_Date_File = open("Today's Date", "w")
        with open("Today_Date_File.csv", "w") as today_date_file:
            write_file_Today_Date_File = csv.writer(today_date_file)
            Menu_Editor = "     Menu Editor:                                             "
            Menu_Editor_File = open("Menu Editor", "w")
        with open("Menu_Editor_File.csv", "w") as menu_editor_file:
            write_file_Menu_Editor_File = csv.writer(menu_editor_file)
            Note_Taking_Program = str(input("Enter your notes here: "))
            Note_Taking_Program_File = open("Note Taking Program", "w")
        with open("Note_Taking_Program_File.csv", "w") as note_taking_program_file:
            write_file_Note_Taking_Program_File = csv.writer(note_taking_program_file)
            print()
            print("Notes")
            print(Menu_Editor)
            print(Your_Name)
            print(Today_Date)
            print("---------------------------------------------------------------------")
            print("Enter your notes here: ")
            print(Note_Taking_Program)
            print()

def Append_File():
    with open("Your_Name_File.csv", "w") as Your_Name_:
        write_file_Your_Name = csv.writer(Your_Name_)
        Today_Date = str(input("Enter today's date: "))
        Today_Date_File = open("Today's Date", "w")
    with open("Today_Date_File.csv", "w") as today_date_file:
        write_file_Today_Date_File = csv.writer(today_date_file)
        Menu_Editor = "     Menu Editor:                                             "
        Menu_Editor_File = open("Menu Editor", "w")
    with open("Menu_Editor_File.csv", "w") as menu_editor_file:
        write_file_Menu_Editor_File = csv.writer(menu_editor_file)
        Note_Taking_Program = str(input("Enter your notes here: "))
        Note_Taking_Program_File = open("Note Taking Program", "w")
    with open("Note_Taking_Program_File.csv", "w") as note_taking_program_file:
        write_file_Note_Taking_Program_File = csv.writer(note_taking_program_file)
        user_input_3 = str(input("Enter (yes/no): Do you want to append the file? "))
        number_of_times_ = int(input("Enter the number of times you want to input variables and print file: "))
        if user_input_3 == "yes":
            for count in range(0, number_of_times_):
                Your_Name_ = str(input("Enter your new name: "))
                Your_Name_File: TextIO = open("Your_Name_", "a")
                Your_Name_File.write(Your_Name_)
                Your_Name_File.close()
                Updated_Today_Date = str(input("Enter today's date: "))
                Updated_Today_Date_File: TextIO = open("Updated_Today_Date", "a")
                Updated_Today_Date_File.write(Updated_Today_Date + "\n")
                Updated_Today_Date_File.close()
                Updated_Note_Taking_Program = str(input("Enter your notes here: "))
                Updated_Note_Taking_Program_File = open("Updated_Note_Taking_Program", "a")
                Updated_Note_Taking_Program_File.write(Updated_Note_Taking_Program)
                Updated_Note_Taking_Program_File.close()
                print()
                print()
                print("Notes")
                print(Menu_Editor)
                print(Your_Name_)
                print(Updated_Today_Date)
                print("---------------------------------------------------------------------")
                print("Enter your notes here: ")
                print(Updated_Note_Taking_Program)
                print()
                

‘’’
Extra Credit:

Allow the user to select a 4th option:

D) Replace a single line

If the user wants to replace a single line in the file, they will then need to be prompted for 2 bits of information:

  1. The line number they want to update.

  2. The text that should replace that line.
    ‘’’

def Replace_Single_Line():
    Your_Name_ = str(input("Enter your name: "))
    Your_Name_File: TextIO = open("Your_Name_", "a")
    Your_Name_File.write(Your_Name_)
    Your_Name_File.close()
    Updated_Today_Date = str(input("Enter today's date: "))
    Updated_Today_Date_File: TextIO = open("Updated_Today_Date", "a")
    Updated_Today_Date_File.write(Updated_Today_Date + "\n")
    Updated_Today_Date_File.close()
    Updated_Note_Taking_Program = str(input("Enter your notes here: "))
    Updated_Note_Taking_Program_File = open("Updated_Note_Taking_Program", "a")
    Updated_Note_Taking_Program_File.write(Updated_Note_Taking_Program)
    Updated_Note_Taking_Program_File.close()
    Menu_Editor = "     Menu Editor:                                             "

    file_name = open("Your_Name_File.csv", "r")
    replaced_variable = ""
    New_Updated_Note_Taking_Program = str(input("Enter your new updated notes: "))

    for line in file_name:
        # stripping line break
        line = line.strip()
        # replacing the texts
        new_line = line.replace(Updated_Note_Taking_Program, New_Updated_Note_Taking_Program)
        # concatenate the new string and add an end-line break
        replaced_variable = replaced_variable + new_line + "\n"
    file_name.close()
    write_file = open("Your_Name_File.csv", "w")
    write_file.write(replaced_variable)
    print()
    print()
    print("Notes")
    print(Menu_Editor)
    print(Your_Name_)
    print(Updated_Today_Date)
    print("---------------------------------------------------------------------")
    print("Enter your notes here: ")
    print(New_Updated_Note_Taking_Program)
    print()
    write_file.close()


while True:
    user_input_ = int(input(
        "Enter a number to choose: (Read the file: 1==1),(Delete the file: 2==2), (Append the file: 3==3), (Replace a single line: 4==4) "))
    if user_input_ == 1:
        Read_The_File()
    elif user_input_ == 2:
        Delete_File_and_Start_Over()
    elif user_input_ == 3:
        Append_File()
    elif user_input_ == 4:
        Replace_Single_Line()

You should format your code as code when posting.

1 Like

If the filename already exists, it should also give them the option to rename it. :wink:

It’s funny how different languages offer advantages in different situations. For example, in MUMPS:

Compare(P1,P2,P3)    ;compare 3 values, return true if any two are equal
    Q:(P1=P2)!(P1=P3)!(P2=P3) 1
    Q 0
    ; end of function

Because MUMPS automatically does conversion between string and numeric values, it doesn’t care whether you pass it strings, integers, or floating-point values; it will perform the appropriate conversions behind the scenes – the one function will compare any three values, so ‘Compare(“Yes”,1,“Yes”)’ will return true.

1 Like