diff --git a/ch_1_Kevin_Rockwell.py b/ch_1_Kevin_Rockwell.py new file mode 100644 index 0000000..1385fe3 --- /dev/null +++ b/ch_1_Kevin_Rockwell.py @@ -0,0 +1 @@ +print("Hello, world!") \ No newline at end of file diff --git a/ch_1_assign_kevin_rockwell.py b/ch_1_assign_kevin_rockwell.py new file mode 100644 index 0000000..8a5a1c5 --- /dev/null +++ b/ch_1_assign_kevin_rockwell.py @@ -0,0 +1,112 @@ +teams = { #Stores all the teams with string form of team number as key + 1: { + "location": "Pontiac, Michigan, USA", + "rookie_year": 1997, + "competed_2019": True, + "2019_comps": { + "FIM District Center Line Event 2019": "Center Line, MI, USA", + "FIM District Troy Event 2019": "Troy, MI, USA", + }, + }, + 16: { + "location": "Mountain Home, Arkansas, USA", + "rookie_year": 1996, + "competed_2019": True, + "2019_comps": { + "Midwest Regional": "Chicago, IL, USA", + "Rocket City Regional 2019": "Huntsville, AL, USA", + "Darwin Division 2019": "Detriot, MI, USA", + }, + "2019_awards": [ + "Industrial Design Award sponsored by General Motors", + "Regional Finalists", + "Excellence in Engineering Award sponsored by Delphi", + ], + }, + 253: { + "location": "Millbrae, California, USA", + "rookie_year": 1999, + "competed_2019": True, + "2019_comps": { + "San Fransisco Regional": "San Fransisco, CA, USA", + "Monterey Bay Regional": "Seaside, CA, USA", + "Newton Divison": "Houston, TX, USA" + }, + "2019_awards": [ + "Team Spirit Award sponsored by FCA Foundation", + ], + }, + 342: { + "location": "North Charleston, South Carolina, USA", + "rookie_year": 2000, + "competed_2019": True, + "2019_comps": { + "Palmetto Regional 2019": "Myrtle Beach, SC, USA", + "Rocket City Regional 2019": "Huntsville, AL, USA", + }, + "2019_awards": [], + }, + 554: { + "location": "Ft. Thomas, Kentucky, USA", + "rookie_year": 2001, + "competed_2019": True, + "2019_comps": { + "Miami Valley Regional 2019": "Fairborn, OH, USA", + }, + "2019_awards": [], + } +} + +#Attributes that can be input as keys, their discription as values +valid_requests = { + "location": "Team's location", + "rookie_year": "Team's rookie year", + "competed_2019": "Seeing if team competed in 2019", + "2019_comps": "A list of the competitions the team competed in for 2019", + "comp_locations": "The location of the 2019 competitions", + "2019_awards": "Awards won during the 2019 season", +} + + +print("Valid teams:") +print(" ".join(map(str, teams.keys()))) #Print teams seperated by spaces onto one line +requested_team_number = "" + +while requested_team_number == "": #loop until valid input + requested_team_number = input("Team Number: ") + if requested_team_number.isdigit(): #Tests if input is only digits + if int(requested_team_number) in teams: + team_number = int(requested_team_number) + else: + print(f"Team number '{requested_team_number}' not stored.") + requested_team_number = "" + + else: + print(f"Invalid team number '{requested_team_number}'") + requested_team_number = "" + + +print("Valid Requests:") +for attribute in valid_requests: #loop through keys in valid_attributes + print(f"'{attribute}' for {valid_requests[attribute]}") #prints name/function +requested_attribute = "" +while requested_attribute == "": #loop until valid input + #Case insensitive input, spaces replaced w/underscore + requested_attribute = input("Attribute: ").lower().replace(" ", "_") + + if requested_attribute not in valid_requests: + print(f"Attribute {requested_attribute} not recognized.") + requested_attribute = "" +if requested_attribute != "comp_locations": + requested_attribute_value = teams[team_number][requested_attribute] +else: + requested_attribute_value = teams[team_number]["2019_comps"] +print(f"Team {requested_team_number}'s {requested_attribute}:") +if isinstance(requested_attribute_value, list): + for value in requested_attribute_value: + print(value) +elif isinstance(requested_attribute_value, dict): + for event in requested_attribute_value: + print(f"{event} - {requested_attribute_value[event]}") +else: + print(requested_attribute_value) \ No newline at end of file diff --git a/ch_2_assign_kevin_rockwell.py b/ch_2_assign_kevin_rockwell.py new file mode 100644 index 0000000..90abf41 --- /dev/null +++ b/ch_2_assign_kevin_rockwell.py @@ -0,0 +1,183 @@ +teams = {} + +def get_team_num(): + print("Enter team number or 'c' to cancel") + while True: + team_num = input("Team Number: ").lower().rstrip() + if team_num.isdigit(): + return int(team_num) + elif team_num.lower() == "c": + return "" + else: + print(f"Invalid Team Number {team_num}") + +def get_vision(): + while True: #Limit Vision to True or False + vision = input("Team Has Vision System [True/False]: ").lower().rstrip() + if vision == "t" or vision == "true": + return True + elif vision == "f" or vision == "false": + return False + elif vision == "": + return "Unknown" + else: + print(f"Invalid Value {vision}, expected True or False") + +def get_drivetrain_motors(): + while True: #Limit Number of motors to int + motors = input("Number of Drivetrain Motors (Integer): ") + if motors.isdigit(): + return int(motors) + elif motors == "": + return "Unknown" + else: + print(f"Invalid Motor Number {motors}") + +def get_input(prompt): + """Takes input, returns None for Blank String""" + response = input(prompt) + if response != "": + return response + else: + return "Unknown" + +def print_team(team_num, team): + print(f"Team {team_num} - {team['name']}\n") #Print Number - Name + print(f"Programming Language: {team['lang']}") + print(f"Robot Length: {team['length']}") + print(f"Robot Width: {team['width']}") + print(f"Has Vision System: {team['vision']}") + print(f"Number of Drivetrain Motors: {team['motors']}") + +def get_team_data(): + temp_team = {} #Store unentered values as blank string + print("Input team data, enter blank line to skip data point") + #Store Unknowns as "Unknown" using get_input + temp_team["name"] = get_input("Team name:\n") + temp_team["lang"] = get_input("Programming Language: ") + + #Keep dimensions as str to allow arbitrary dimensions + temp_team["width"] = get_input("Robot Width: ") + temp_team["length"] = get_input("Robot Length: ") + + temp_team["vision"] = get_vision() + temp_team["motors"] = get_drivetrain_motors() + return temp_team + +while True: + #Print main menu + print(""" + Allowed Actions: + (a)dd Team + (v)iew team data + (m)odify team data + (r)emove team + (s)earch for teams + (l)ist all teams + (e)xit + """) + selection = "" + selection = input("Action: ") + if selection == "a": #Add team + team_num = get_team_num() + if team_num == "": #User canceled add operation + continue #Go back into main menu loop + elif team_num in teams.keys(): + print(f"Team Number {team_num} already in ") + temp_team = get_team_data() + + while True: #Confirm before saving + save = input(f"Save team {team_num}? [Y/n]").lower() + if save == "y" or save == "": + teams[team_num] = temp_team + break + elif save == "n": + break + + elif selection == "v": #View team + team_num = get_team_num() + print_team(team_num, teams[team_num]) + + elif selection == "m": #modify team + team_num = get_team_num() + if team_num not in teams.keys(): + print(f"Team Number {team_num} not stored.") + continue + print(f"Modifying team {team_num}") + while True: #Confirm before saving + print(""" + Select Attribute to Modify, or 'e' to return to main menu: + (n)ame + (p)rogramming language + (w)idth + (l)ength + (v)ision + (m)otor numbers in drietrain + """) + to_modify = input("Selection: ") + if to_modify == "n": + teams[team_num]["name"] = get_input("Enter Name: ") + elif to_modify == "p": + teams[team_num]["lang"] = get_input("Enter Language: ") + elif to_modify == "w": + teams[team_num]["width"] = get_input("Enter Width: ") + elif to_modify == "l": + teams[team_num]["length"] = get_input("Enter Length: ") + elif to_modify == "v": + teams[team_num]["vision"] = get_vision() + elif to_modify == "m": + teams[team_num]["motors"] = get_drivetrain_motors() + elif to_modify == "e": + break + else: + print("Invalid Action") + + elif selection == "r": #remove team + team_num = get_team_num() + if team_num != "": + if team_num in teams.keys(): + del teams[int(team_num)] + else: + print(f"Invalid Team Number {team_num}") + else: + continue #user canceled, so return to main menu + + elif selection == "s": #search for teams + search_str = input("Search for team by name or number: ") + matches = [] + if search_str.isdigit(): + for team, dat in teams.items(): + if str(team).startswith(search_str): #if start if numbers match + matches.append(f"{team}: {dat['name']}") #number + name + else: + for team, dat in teams.items(): + if dat["name"].startswith(search_str): #if start of name match + matches.append(f"{team}: {dat['name']}") #num + name + + if matches != []: + print(f"Matches to {search_str}") + for m in matches: + print(m) + else: + print(f"No matches for {search_str}") + + elif selection == "l": #list teams + if len(teams.keys()) < 1: + print("No stored teams") + continue + print("Stored Teams:") + temp_ls = [] #used to store numbers temporarily to print nicer + for team in teams.keys(): + if len(temp_ls) < 4: #print 4 numbers per line + temp_ls.append(str(team)) + else: + print(" ".join(temp_ls)) + temp_ls = [str(team)] + else: + print(" ".join(temp_ls)) + + elif selection == "e": #exit program + break + + else: + print(f"Invalid Action: {selection}\n") \ No newline at end of file diff --git a/ch_3_assign_kevin_rockwell.py b/ch_3_assign_kevin_rockwell.py new file mode 100644 index 0000000..94cbb26 --- /dev/null +++ b/ch_3_assign_kevin_rockwell.py @@ -0,0 +1,219 @@ +teams = {} + +def input_or_cancel(prompt): + i = input(prompt) + if i == "q": + return None + else: + return i + +def get_positive_int(): + while True: + i = input_or_cancel("Enter a positive integer or 'q' to cancel: ") + if i is not None: + if i.isdigit(): + return int(i) + else: + print(f"Invalid input {i}") + continue + return i # user canceled, so return none + +def get_bool(): + while True: + i = input_or_cancel("Please enter (y)es or (n)o: ") + if i is not None: + i = i.lower() + if i in ["t", "true", "1", "y", "yes"]: # Accept more truthy values + return True + elif i in ["f", "false", "0", "n", "no"]: # Same but for falsey + return False + else: + print(f"Invalid input {i}") + continue + return i # User canceled + +def get_comps(): + comps = {} + while True: + if comps == {}: # No comps have been, so add + print("Add competition?") + else: # Comps have been added, so add another comp + print("Add another competition?") + if not get_bool(): #User does not want to add more competitions + return comps + + name = input_or_cancel("Input Competition Name or 'q' to cancel:\n") + if name is None: + continue + elif name in comps: + print(f"Competition {name} already in list") + continue + + location = input_or_cancel("Input Location or 'q' to cancel:\n") + if location is None: + continue + comps[name] = location + +def print_team(team, team_num): + print(f"Team Number {team_num}") + for a in attributes: + value = team.get(a) + if value is not None: + if a == "2019_comps": + print(a + ":") + if value == {}: + print(f"No stored competitions for {team_num}") + continue + for comp, location in value.items(): + print(f"{comp} in {location}") + continue + print(f"{a}: {value}") + else: + print(f"{a} is unknown for team {team_num}") + +attributes = [ + "location", + "rookie_year", + "competed_2019", + "2019_comps", + "2019_awards", + ] + +while True: + #Main Menu + print(""" + Allowed Actions: + (a)dd Team + (v)iew team data + (m)odify team data + (r)emove team + (s)earch for teams + (l)ist all teams + (e)xit + """) + selection = "" + selection = input("Action: ") + if selection == "a": #Add team + print("Enter team Number:") + team_num = get_positive_int() + if team_num in teams: + print(f"Team Number {team_num} already in teams") + continue + elif team_num is None: + continue #User canceled, so exit add team and return to main menu + temp_team = {} + temp_team["location"] = input_or_cancel("Input team location or" + + " 'q' if unknown: ") # None for unknowns + print("Enter team rookie year") + temp_team["rookie_year"] = get_positive_int() + print("Enter if team competed in 2019") + temp_team["competed_2019"] = get_bool() + temp_team["2019_comps"] = get_comps() + temp_team["2019_awards"] = input_or_cancel("Enter 2019 awards or" + + " 'q' if unknown\n") + + print(f"Save team {team_num}") + if get_bool(): + teams[team_num] = temp_team + + elif selection == "v": #View team + print("Input team number: ") + team_num = get_positive_int() + if team_num not in teams: + print(f"Team Number {team_num} not stored.") + continue + elif team_num == None: + continue + print_team(teams[team_num], team_num) + + elif selection == "m": #modify team + print("Input team number:") + team_num = get_positive_int() + if team_num is None: + continue + elif team_num not in teams: + print(f"Team number {team_num} not stored.") + continue + print("Current Values:") + print_team(teams[team_num], team_num) #Show current values + print(""" + Attribute to change: + (l)ocation + (r)ookie year + (c)ompetitions in 2019 + (i)f the team competed in 2019 + (a)wards won in 2019 + """) + while True: + i = input_or_cancel("Select attribute or 'q' to cancel: ") + if i in ["l", "r", "c", "i", "a", None]: + break + else: + print("Invalid attribute selection.") + if i == None: + continue + if i == "l": # Change location + location = input_or_cancel("Enter updated location or 'q' to cancel: ") + if location is not None: + teams[team_num]["location"] = location + elif i == "r": # Change rookie year + rookie = input_or_cancel("Enter updated rookie year or 'q' to cancel: ") + if rookie is not None: + teams[team_num]["rookie_year"] = rookie + elif i == "c": # Change 2019 competitions + print("Please enter all 2019 competitions. Enter no competitions to cancel") + comps = get_comps() + if comps != {}: + teams[team_num]["2019_comps"] = comps + elif i == "i": # Change competed 2019 + print("Enter if team competed in 2019 or 'q' to cancel") + comp_2019 = get_bool() + if comp_2019 is not None: + teams[team_num]["competed_2019"] = comp_2019 + elif i == "a": + awards = input_or_cancel("Enter 2019 awards or 'q' to cancel") + if awards is not None: + teams[team_num]["2019_awards"] = awards + + elif selection == "r": #remove team + print("Input team number:") + team_num = get_positive_int() + if team_num is None: + continue + elif team_num not in teams: + print(f"Team Number {team_num} not stored.") + continue + + print(f"Delete team {team_num}?") + if get_bool(): + del teams[team_num] + + elif selection == "s": #search for teams + print("Enter the start of or complete team number to search for:") + search_team = get_positive_int() + for t in teams: + if str(t).startswith(str(search_team)): + print(t) + + elif selection == "l": #list teams + if len(teams.keys()) < 1: + print("No stored teams") + continue + print("Stored Teams:") + temp_ls = [] #used to store numbers temporarily to print nicer + for team in teams.keys(): + if len(temp_ls) < 4: #print 4 numbers per line + temp_ls.append(str(team)) + else: + print(" ".join(temp_ls)) + temp_ls = [str(team)] + else: + print(" ".join(temp_ls)) + + elif selection == "e": #exit program + print("Confirm Exit:") + if get_bool(): + break + + else: + print(f"Invalid Action: {selection}\n") \ No newline at end of file diff --git a/ch_4_assign_kevin_rockwell.py b/ch_4_assign_kevin_rockwell.py new file mode 100644 index 0000000..4a5a4d4 --- /dev/null +++ b/ch_4_assign_kevin_rockwell.py @@ -0,0 +1,65 @@ +from math import hypot + +class Point(): + def __init__(self, x, y): + self.x, self.y = x, y + + def distance(self): + return hypot(self.x, self.y) + + +class Point_3D(Point): + def __init__(self, x, y, z): + super().__init__(x, y) + self.z = z + + def distance(self): + return hypot(super().distance(), self.z) + + +def get_num(): + while True: + i = input("Enter a real number: ") + sign = "" + if i[0] in ["+", "-"]: # Has a sign as first value + sign = i[0] + i = i[1:] #Number without the sign + decimal = False # Used to track if we've had a decimal point + for c in i: + if c not in "1234567890": + if c == "." and not decimal: + decimal = True + else: + print(f"Invalid input {i}") + break + else: + return float(sign + i) + +if __name__ == "__main__": + print("Enter x value for first point") + x = get_num() + print("Enter y value for first point") + y = get_num() + two_d = Point(x, y) + print("Enter x value for second point") + x = get_num() + print("Enter y value for second point") + y = get_num() + print("Enter z value for second point\n") + z = get_num() + three_d = Point_3D(x, y, z) + + + if two_d.distance() > three_d.distance(): + print("The first point is further from the origin") + elif two_d.distance() < three_d.distance(): + print("The second point is further from the origin") + else: + print("The two points are equally far from the origin") + + if two_d.distance() > Point.distance(three_d): #Point to only use x and y + print("The second point is further from origin in x and y") + elif two_d.distance() < Point.distance(three_d): + print("The first point is further from origin in x and y") + else: + print("The two points are equally far from the origin in x and y")