diff --git a/day_01/01_basics/exercise/01_hello_v2.py b/day_01/01_basics/exercise/01_hello_v2.py index c3c7aab..c6b8fec 100644 --- a/day_01/01_basics/exercise/01_hello_v2.py +++ b/day_01/01_basics/exercise/01_hello_v2.py @@ -1,4 +1,9 @@ # TODO: Print the following in the console: # Use variables for the name and programming language -# print("Hello! My name is Jeff") -# print("I am learning Python") +print("Hello! My name is Leng") +print("I am learning Python") + +name = "Leng" +language = "Python" +print("Hello! My name is", name) +print("I am learning", language) \ No newline at end of file diff --git a/day_01/01_basics/exercise/02_counter.py b/day_01/01_basics/exercise/02_counter.py index b516c84..ec399f0 100644 --- a/day_01/01_basics/exercise/02_counter.py +++ b/day_01/01_basics/exercise/02_counter.py @@ -2,13 +2,16 @@ print("Counter:", counter) # Point up: Add one to the counter -# TODO: Change code here +# counter = counter + 1 +counter += 1 print("Counter:", counter) # Bonus: Multiply the score by 10 -# TODO: Change code here +# counter = counter * 10 +counter *= 10 print("Counter:", counter) # Penalty: Decrease the score by 4 -# TODO: Change code here +# counter = counter - 4 +counter -= 4 print("Counter:", counter) diff --git a/day_01/01_basics/exercise/02_counter_v2.py b/day_01/01_basics/exercise/02_counter_v2.py index afadbcf..bb5e8a1 100644 --- a/day_01/01_basics/exercise/02_counter_v2.py +++ b/day_01/01_basics/exercise/02_counter_v2.py @@ -3,12 +3,15 @@ # Point up: Add one to the counter # TODO: Change code here +counter += 1 print("Counter:", counter) # Bonus: Multiply the score by 10 # TODO: Change code here +counter *= 10 print("Counter:", counter) # Penalty: Decrease the score 4 # TODO: Change code here +counter -= 4 print("Counter:", counter) diff --git a/day_01/01_basics/exercise/03_wishlist.py b/day_01/01_basics/exercise/03_wishlist.py index 16a3590..6ab23b9 100644 --- a/day_01/01_basics/exercise/03_wishlist.py +++ b/day_01/01_basics/exercise/03_wishlist.py @@ -1,7 +1,7 @@ # TODO: Fill in the variables based on the item you want to buy -name = None # What is the name of the item? -price = None # How much is the item? -organic = None # Is it organic? +name = "iPhone 17" # What is the name of the item? +price = 100_000 # How much is the item? +organic = True # Is it organic? # TODO: Then, print each information one line at a time print(name) diff --git a/day_01/01_basics/exercise/04_expense_tracker_v2.py b/day_01/01_basics/exercise/04_expense_tracker_v2.py index de29a3d..39b0b47 100644 --- a/day_01/01_basics/exercise/04_expense_tracker_v2.py +++ b/day_01/01_basics/exercise/04_expense_tracker_v2.py @@ -1,13 +1,15 @@ # TODO: Ask the user for three values -expense_1 = None # Let the user enter a number -expense_2 = None # Let the user enter a number -expense_3 = None # Let the user enter a number +expense_1 = int(input("Expense_1: ")) # Let the user enter a number +expense_2 = int(input("Expense_2: ")) # Let the user enter a number +expense_3 = int(input("Expense_3: ")) # Let the user enter a number # TODO: Then, print each information one line at a time -print(expense_1) -print(expense_2) -print(expense_3) +print("Expense_1 :" , expense_1) +print("Expense_2 :" , expense_2) +print("Expense_3 :" , expense_3) + + # TODO: Calculate the sum of the numbers -total = None -print(total) +total = int(expense_1) + int(expense_2) + int(expense_3) +print("Total:" , total) diff --git a/day_01/01_basics/exercise/04_expense_tracker_v3.py b/day_01/01_basics/exercise/04_expense_tracker_v3.py index 532b109..7e22de2 100644 --- a/day_01/01_basics/exercise/04_expense_tracker_v3.py +++ b/day_01/01_basics/exercise/04_expense_tracker_v3.py @@ -1,15 +1,16 @@ # TODO: Ask the user for three values -expense_1 = None # Let the user enter a number -expense_2 = None # Let the user enter a number -expense_3 = None # Let the user enter a number +expense_1 = int(input("Expense_1: ")) # Let the user enter a number +expense_2 = int(input("Expense_2: ")) # Let the user enter a number +expense_3 = int(input("Expense_3: ")) # Let the user enter a number # TODO: Then, print each information one line at a time -print(expense_1) -print(expense_2) -print(expense_3) +print("Expense_1 :" , expense_1) +print("Expense_2 :" , expense_2) +print("Expense_3 :" , expense_3) total = expense_1 + expense_2 + expense_3 print(total) # TODO: Format this part using f-strings -print(expense_1, "+", expense_2, "+", expense_3, "=", total) +print(f"Expense_1: {expense_1} + Expense_2: {expense_2} + Expense_3: {expense_3} = {total}") + diff --git a/day_01/01_basics/exercise/05_ice_ice_ice_baby.py b/day_01/01_basics/exercise/05_ice_ice_ice_baby.py index 91c13b2..3e85f1b 100644 --- a/day_01/01_basics/exercise/05_ice_ice_ice_baby.py +++ b/day_01/01_basics/exercise/05_ice_ice_ice_baby.py @@ -1,5 +1,5 @@ -ice = "Ice" +ice = "Ice " *3 baby = "Baby" # TODO: Print "Ice Ice Ice Baby" using + and * -print() +print(ice + baby) diff --git a/day_01/01_basics/exercise/06_price_post.py b/day_01/01_basics/exercise/06_price_post.py index cdd5a5a..10187c8 100644 --- a/day_01/01_basics/exercise/06_price_post.py +++ b/day_01/01_basics/exercise/06_price_post.py @@ -2,10 +2,13 @@ price_notification = "The price of {} is ${}." # TODO: Post: Latte ($3.5) -print(price_notification) +formatted_price_notification = price_notification.format("Latte", 3.5) +print(formatted_price_notification) # TODO: Post: Espresso ($2.75) -print(price_notification) +formatted_price_notification = price_notification.format("Espresso", 2.75) +print(formatted_price_notification) # TODO: Post: Cappuccino ($4.0) -print(price_notification) \ No newline at end of file +formatted_price_notification = price_notification.format("Cappuccino", 4.0) +print(formatted_price_notification) \ No newline at end of file diff --git a/day_01/01_basics/exercise/07_sales_tracker.py b/day_01/01_basics/exercise/07_sales_tracker.py index b736fdc..a1ba7b3 100644 --- a/day_01/01_basics/exercise/07_sales_tracker.py +++ b/day_01/01_basics/exercise/07_sales_tracker.py @@ -1,17 +1,12 @@ -# Ask the cost and pax or count for three separate items -item_cost_1 = int(input("Item cost 1: ")) -item_count_1 = int(input("Item count 1: ")) +item_cost_1 = int(input("Item cost 1: ")) # Let the user enter a number +item_count_1 = int(input("Item count 1: ")) # Let the user enter a number -item_cost_2 = int(input("Item cost 2: ")) -item_count_2 = int(input("Item count 2: ")) +item_cost_2 = int(input("Item cost 2: ")) # Let the user enter a number +item_count_2 = int(input("Item count 2: ")) # Let the user enter a number -item_cost_3 = int(input("Item cost 3: ")) -item_count_3 = int(input("Item count 3: ")) +item_cost_3 = int(input("Item cost 3: ")) # Let the user enter a number +item_count_3 = int(input("Item count 3: ")) # Let the user enter a number -# Calculate the total -total = ( - (item_cost_1 * item_count_1) - + (item_cost_2 * item_count_2) - + (item_cost_3 * item_count_3) -) -print(total) +#calculate total sales +total_sales = (item_cost_1 * item_count_1) + (item_cost_2 * item_count_2) + (item_cost_3 * item_count_3) +print("Total sales: ", total_sales) diff --git a/day_01/01_basics/exercise/08_height_requirment.py b/day_01/01_basics/exercise/08_height_requirment.py new file mode 100644 index 0000000..1c90ffa --- /dev/null +++ b/day_01/01_basics/exercise/08_height_requirment.py @@ -0,0 +1,11 @@ +minimum_height =138 + +# Ask the user for the following inputs +user_height = int(input("Enter your height in cm: ")) + +# Notify user if they can enter the ride +can_enter_ride = user_height >= minimum_height +print("You can enter the ride") + +if not can_enter_ride: + print("Sorry, you have to grow taller before you can ride.") \ No newline at end of file diff --git a/day_01/01_basics/exercise/09_valid_score.py b/day_01/01_basics/exercise/09_valid_score.py new file mode 100644 index 0000000..379a236 --- /dev/null +++ b/day_01/01_basics/exercise/09_valid_score.py @@ -0,0 +1,10 @@ +# Range minimum and maximum bounds +min_score = 0 +max_score = 100 + +# ENter user input +number = int(input("Enter your score: ")) + +# Notify user if the number is a valid score +valid_score = min_score <= number <= max_score +print("Valid score:", valid_score) \ No newline at end of file diff --git a/day_01/01_basics/exercise/10_login_system.py b/day_01/01_basics/exercise/10_login_system.py new file mode 100644 index 0000000..f6daf38 --- /dev/null +++ b/day_01/01_basics/exercise/10_login_system.py @@ -0,0 +1,10 @@ +# Expected password +correct_password = "pass" + +# Enter user password +password_input = input("Please provide password: ") + +# Notify user if the password is valid +correct_password_given = password_input == correct_password +print("Access:", correct_password_given) + diff --git a/day_01/01_basics/exercise/10_login_system_v2.py b/day_01/01_basics/exercise/10_login_system_v2.py new file mode 100644 index 0000000..0ac1444 --- /dev/null +++ b/day_01/01_basics/exercise/10_login_system_v2.py @@ -0,0 +1,11 @@ +# Expected password +correct_password = "pass" + +# Enter user password +password_input = input("Please provide password: ") + +# TODO: Notify user if the password is valid +correct_password_given = correct_password == password_input + +if correct_password_given: + print("Access granted") \ No newline at end of file diff --git a/day_01/01_basics/notes/05_input/01_input.py b/day_01/01_basics/notes/05_input/01_input.py index 8339c34..4d67c80 100644 --- a/day_01/01_basics/notes/05_input/01_input.py +++ b/day_01/01_basics/notes/05_input/01_input.py @@ -1,2 +1,4 @@ user_input = input() print(user_input) + + diff --git a/day_01/01_basics/notes/05_input/sample.py b/day_01/01_basics/notes/05_input/sample.py new file mode 100644 index 0000000..124c1dc --- /dev/null +++ b/day_01/01_basics/notes/05_input/sample.py @@ -0,0 +1,28 @@ +password = input("Enter your password: ") +while password != "pass": + print("Incorrect password, try again.") + password = input("Enter your password: ") + + +def greetings(name): + print(f"Hello name ! Nice to meet you.") + +greetings(name) + +def product(x, y, z=1): + result = x * y * z + print(f"The product is: {result}") + + +def product(x, y, z=1): + result = x * y * z + return result + +output = product(x=1, y=2, z=3) * 5 +print(f"The product is: {output}") + + +try: + number_input = int(input("Number:")) +except: + print("Invalid input") diff --git a/day_01/02_control_flow/exercise/07_sales_tracker_v2.py b/day_01/02_control_flow/exercise/07_sales_tracker_v2.py index 070e9e4..ef74f18 100644 --- a/day_01/02_control_flow/exercise/07_sales_tracker_v2.py +++ b/day_01/02_control_flow/exercise/07_sales_tracker_v2.py @@ -1,10 +1,13 @@ # TODO: Ask the user how many items will be calculated -input_count = None +input_count = int(input("How many items will you enter? ")) total = 0 # TODO: Use a for loop to ask for more than one cost and count -item_cost = None # Let the user enter a number -item_count = None # Let the user enter a number -item_total = item_cost + item_count +for i in range(input_count): + item_cost = int(input(f"Enter cost of item {i+1}: ")) + item_count = int(input(f"Enter quantity of item {i+1}: ")) + item_total = item_cost * item_count + total += item_total + +print(f"The total cost is: {total}") -print(total) diff --git a/day_01/02_control_flow/exercise/10_login_system_v3.py b/day_01/02_control_flow/exercise/10_login_system_v3.py index 7bcc33f..03b1a7e 100644 --- a/day_01/02_control_flow/exercise/10_login_system_v3.py +++ b/day_01/02_control_flow/exercise/10_login_system_v3.py @@ -5,6 +5,6 @@ password_input = input("Please provide password: ") # TODO: Notify user if password is valid or invalid -correct_password_given = None +correct_password_given = correct_password == password_input print("Access Granted") print("Access Denied") diff --git a/day_01/02_control_flow/exercise/10_login_system_v4.py b/day_01/02_control_flow/exercise/10_login_system_v4.py index a261f00..f4e70d1 100644 --- a/day_01/02_control_flow/exercise/10_login_system_v4.py +++ b/day_01/02_control_flow/exercise/10_login_system_v4.py @@ -7,6 +7,8 @@ password_input = input("Please provide password: ") # TODO: Notify user if credentials are valid or invalid -correct_credentials = None -print("Access Granted") -print("Access Denied") +correct_credentials = (correct_username == username_input) * (correct_password == password_input) +if correct_credentials: + print("Access Granted") +else: + print("Access Denied") diff --git a/day_01/02_control_flow/exercise/10_login_system_v5.py b/day_01/02_control_flow/exercise/10_login_system_v5.py index d93ae98..56df8bb 100644 --- a/day_01/02_control_flow/exercise/10_login_system_v5.py +++ b/day_01/02_control_flow/exercise/10_login_system_v5.py @@ -9,6 +9,8 @@ password_input = input("Please provide password: ") # TODO: Notify user if credentials are valid or invalid -correct_credentials = None -print("Access Granted") -print("Access Denied") +correct_credentials = ((correct_username == username_input) * (correct_password == password_input)) or ((admin_username == username_input) * (admin_password == password_input)) +if correct_credentials: + print("Access Granted") +else: + print("Access Denied") \ No newline at end of file diff --git a/day_01/02_control_flow/exercise/11_traffic_lights.py b/day_01/02_control_flow/exercise/11_traffic_lights.py index d360237..c234cd3 100644 --- a/day_01/02_control_flow/exercise/11_traffic_lights.py +++ b/day_01/02_control_flow/exercise/11_traffic_lights.py @@ -5,3 +5,22 @@ # "green" -> print "Go" # "yellow" -> print "Wait..." # "red" -> print "Stop" + +if color_input == "green": + print("Go") +elif color_input == "yellow": + print("Wait...") +elif color_input == "red": + print("Stop") + + +match color_input: + case "green": + print("Go") + case "yellow": + print("Wait...") + case "red": + print("Stop") + + + diff --git a/day_01/02_control_flow/exercise/11_traffic_lights_v2.py b/day_01/02_control_flow/exercise/11_traffic_lights_v2.py index bac9698..01b0ed7 100644 --- a/day_01/02_control_flow/exercise/11_traffic_lights_v2.py +++ b/day_01/02_control_flow/exercise/11_traffic_lights_v2.py @@ -6,3 +6,22 @@ # "yellow" -> print "Wait..." # "red" -> print "Stop" # Everything else -> print "Malfunction" + +if color_input == "green": + print("Go") +elif color_input == "yellow": + print("Wait...") +elif color_input == "red": + print("Stop") +else: + print("Malfunction") + +match color_input: + case "green": + print("Go") + case "yellow": + print("Wait...") + case "red": + print("Stop") + case _: + print("Malfunction") \ No newline at end of file diff --git a/day_01/02_control_flow/exercise/12_bookmarks_v2.py b/day_01/02_control_flow/exercise/12_bookmarks_v2.py index 3cd94ec..c837af1 100644 --- a/day_01/02_control_flow/exercise/12_bookmarks_v2.py +++ b/day_01/02_control_flow/exercise/12_bookmarks_v2.py @@ -1,4 +1,10 @@ # TODO: Define a list of your favorite websites -websites = [] +websites = ["facebook.com", "instagram.com", "twitter.com", "linkedIn.com", "github.com"] # TODO: Print the entire list of websites (one at a time) +for site in websites: + print(site) + +numbers = [0, 1, 2] +for number in range(3): + print (number) \ No newline at end of file diff --git a/day_01/02_control_flow/exercise/13_repetition.py b/day_01/02_control_flow/exercise/13_repetition.py index 5797a8c..6077c44 100644 --- a/day_01/02_control_flow/exercise/13_repetition.py +++ b/day_01/02_control_flow/exercise/13_repetition.py @@ -2,4 +2,6 @@ message = "This is a very long message that's hard to type" # TODO: Print the message eleven times -print(message) \ No newline at end of file +print(message) +for x in range(11): + print(message) diff --git a/day_01/02_control_flow/exercise/14_counting.py b/day_01/02_control_flow/exercise/14_counting.py index 1f9eda5..dfd2d65 100644 --- a/day_01/02_control_flow/exercise/14_counting.py +++ b/day_01/02_control_flow/exercise/14_counting.py @@ -2,4 +2,6 @@ end = int(input("Enter number: ")) # TODO: Print the numbers 0 to end -print() +range_numbers = range(end + 1) +for number in range_numbers: + print(number) diff --git a/day_01/02_control_flow/exercise/16_running_balance.py b/day_01/02_control_flow/exercise/16_running_balance.py index f3bd3be..f09240d 100644 --- a/day_01/02_control_flow/exercise/16_running_balance.py +++ b/day_01/02_control_flow/exercise/16_running_balance.py @@ -4,8 +4,29 @@ command = input("Provide command: ") if command == "add": - pass # TODO: Ask for number, add to total, and print + number = int(input("Enter a number: ")) + total += number + print("Current total is:", total) + if command == "sub": - pass # TODO: Ask for number, subtract to total, and print + number = int(input("Enter a number: ")) + total -= number + print("Current total is:", total) + elif command == "exit": running = False + +# Using match-case +match command: + case "add": + number = int(input("Enter a number: ")) + total += number + print("Current total is:", total) + + case "sub": + number = int(input("Enter a number: ")) + total -= number + print("Current total is:", total) + case "exit": + running = False + diff --git a/day_01/02_control_flow/exercise/17_search.py b/day_01/02_control_flow/exercise/17_search.py index ae94f66..77d6ed8 100644 --- a/day_01/02_control_flow/exercise/17_search.py +++ b/day_01/02_control_flow/exercise/17_search.py @@ -3,4 +3,7 @@ for item in items: # TODO: If item equals the item_to_find, print and exit loop - pass + if item == item_to_find: + print(f"Found {item_to_find}!") + break + diff --git a/day_01/02_control_flow/exercise/18_skip_range.py b/day_01/02_control_flow/exercise/18_skip_range.py index f4fac19..4da802e 100644 --- a/day_01/02_control_flow/exercise/18_skip_range.py +++ b/day_01/02_control_flow/exercise/18_skip_range.py @@ -1,3 +1,7 @@ for item in range(100): # TODO: Change code to skip printing numbers 20 to 80. + if item in (20, 80): + continue print(item) + + diff --git a/day_01/02_control_flow/exercise/19_line_generator.py b/day_01/02_control_flow/exercise/19_line_generator.py new file mode 100644 index 0000000..f0b5a97 --- /dev/null +++ b/day_01/02_control_flow/exercise/19_line_generator.py @@ -0,0 +1,16 @@ +""" + Crete a function line_generator that prints the following: + Line 1 + Line 2 + Line 3 + . . . + Line number +""" + +#Use the fuction once +def line_generator(): + line_number = int(input("Enter a number: ")) + for i in range(1, line_number + 1): + print(f"Line {i}") +line_generator() + diff --git a/day_01/02_control_flow/exercise/19_line_generator_v2.py b/day_01/02_control_flow/exercise/19_line_generator_v2.py new file mode 100644 index 0000000..584065b --- /dev/null +++ b/day_01/02_control_flow/exercise/19_line_generator_v2.py @@ -0,0 +1,18 @@ +""" + Crete a function line_generator that has a paramaeter number + and prints the following: + Line 1 + Line 2 + Line 3 + . . . + Line number +""" + +#Use the fuction once +def line_generator(number): + for i in range(1, number + 1): + print(f"Line {i}") +line_generator(4) + + + diff --git a/day_01/02_control_flow/notes/04_for_loop/01_list.py b/day_01/02_control_flow/notes/04_for_loop/01_list.py index 62c68c2..85da8f6 100644 --- a/day_01/02_control_flow/notes/04_for_loop/01_list.py +++ b/day_01/02_control_flow/notes/04_for_loop/01_list.py @@ -1,2 +1,10 @@ items = ["milk", "egg", "ice"] -print(items) +for item in items: + print(item) + + + + + + + diff --git a/day_01/03_functions/exercise/19_line_generator_v2.py b/day_01/03_functions/exercise/19_line_generator_v2.py index e07e204..5890545 100644 --- a/day_01/03_functions/exercise/19_line_generator_v2.py +++ b/day_01/03_functions/exercise/19_line_generator_v2.py @@ -1,9 +1,16 @@ """ - TODO: Create a function `line_generator` that has a parameter `number` and prints the following: - Line 1 - Line 2 - ... - Line number + Create a function line_generator that has a paramaeter number + and prints the following: + Line 1 + Line 2 + Line 3 + . . . + Line number """ -# TODO: Use the function once +#Use the fuction once +def line_generator(number): + for i in range(1, number + 1): + print(f"Line {i}") +line_generator(4) + \ No newline at end of file diff --git a/day_01/03_functions/exercise/20_product_v2.py b/day_01/03_functions/exercise/20_product_v2.py index 4a552ce..ff1517c 100644 --- a/day_01/03_functions/exercise/20_product_v2.py +++ b/day_01/03_functions/exercise/20_product_v2.py @@ -1,8 +1,18 @@ -def product(): - """ TODO: Takes three inputs (or two) and print the product""" +def product(num1, num2, num3): + """ TODO: Takes three inputs and print the product""" # TODO: product(1, 1, 1) # 1 # TODO: product(1, 2, 3) # 6 # TODO: product(2, 5, 10) # 100 # TODO: product(3, 3) # 9 # TODO: product(2, 5) # 12 + + + result = num1 * num2 * num3 + print(f"The product is: {result}") + +product1 = int(input("Enter first number: ")) +product2 = int(input("Enter second number: ")) +product3 = int(input("Enter third number: ")) +product(product1, product2, product3) + diff --git a/day_01/03_functions/exercise/20_product_v3.py b/day_01/03_functions/exercise/20_product_v3.py index c5c8e73..bbcae9b 100644 --- a/day_01/03_functions/exercise/20_product_v3.py +++ b/day_01/03_functions/exercise/20_product_v3.py @@ -6,3 +6,13 @@ def product(): # TODO: product(2, 5, 10) # 100 # TODO: product(3, 3) # 9 # TODO: product(2, 5) # 12 + +product1 = int(input("Enter first number: ")) +product2 = int(input("Enter second number: ")) +product3 = int(input("Enter third number: ")) +def product(num1, num2, num3=1): + result = num1 * num2 * num3 + return result +output = product(product1, product2, product3) * 5 +print(f"The product is: {output}") + diff --git a/day_01/05_lab_session/exercise/01_multiplication_table.py b/day_01/05_lab_session/exercise/01_multiplication_table.py index f88423a..beac0b9 100644 --- a/day_01/05_lab_session/exercise/01_multiplication_table.py +++ b/day_01/05_lab_session/exercise/01_multiplication_table.py @@ -17,3 +17,7 @@ 3 x 9 = 27 3 x 10 = 30 """ +for i in range(1, 11): + result = number * i + print(f"{number} x {i} = {result}") + diff --git a/day_01/05_lab_session/exercise/02_positive_input.py b/day_01/05_lab_session/exercise/02_positive_input.py index f46dd63..5351ade 100644 --- a/day_01/05_lab_session/exercise/02_positive_input.py +++ b/day_01/05_lab_session/exercise/02_positive_input.py @@ -3,11 +3,52 @@ # TODO: Then try to convert this into an integer using the following: number_converted = int(number) +print(f"You entered a valid positive integer: {number_converted}") # The user could provide an invalid integer input (string) # TODO: Handle this case +invalid_input = True +try: + number_converted = int(number) + invalid_input = False +except: + print("Invalid input, please provide a valid integer.") +if not invalid_input: + if number_converted < 0: + print("The number is negative, please provide a positive integer.") + else: + print(f"You entered a valid positive integer: {number_converted}") + # The user could give a negative number # TODO: Handle this case +negative_input = True +if not invalid_input: + if number_converted < 0: + print("The number is negative, please provide a positive integer.") + else: + negative_input = False + print(f"You entered a valid positive integer: {number_converted}") + # Challenge: TODO: Give the user infinite times to retry +infinite_retry = True +while infinite_retry: + if not invalid_input and not negative_input: + infinite_retry = False + else: + number = input("Enter number: ") + try: + number_converted = int(number) + invalid_input = False + except: + print("Invalid input, please provide a valid integer.") + continue + if number_converted < 0: + print("The number is negative, please provide a positive integer.") + continue + else: + negative_input = False + print(f"You entered a valid positive integer: {number_converted}") + infinite_retry = False + diff --git a/day_01/05_lab_session/exercise/03_quick_draw.py b/day_01/05_lab_session/exercise/03_quick_draw.py index 3986259..27848be 100644 --- a/day_01/05_lab_session/exercise/03_quick_draw.py +++ b/day_01/05_lab_session/exercise/03_quick_draw.py @@ -1,11 +1,62 @@ # TODO: Ask the user for an input user_choice = input("Pick a choice (rock/paper/scissors): ") +input_choices = ["rock", "paper", "scissors"] +if user_choice not in input_choices: + print("Invalid choice, please pick rock, paper, or scissors.") + exit() + # TODO: Make a random choice for the computer # Note: Read the slide for this part -cpu_choice = None +import random +cpu_choice = random.choice(input_choices) +print(f"Computer picked: {cpu_choice}") + # TODO: Determine if the user wins, the cpu wins, or its a draw +if user_choice == cpu_choice: + print("It's a draw!") +elif (user_choice == "rock" and cpu_choice == "scissors") or \ + (user_choice == "paper" and cpu_choice == "rock") or \ + (user_choice == "scissors" and cpu_choice == "paper"): + print("You win!") +else: + print("Computer wins!") + # Challenge: TODO: Robust Input +robust_input = True +while robust_input: + if user_choice in input_choices: + robust_input = False + else: + user_choice = input("Pick a choice (rock/paper/scissors): ") + if user_choice not in input_choices: + print("Invalid choice, please pick rock, paper, or scissors.") + continue + else: + robust_input = False + # Challenge: TODO: Multi-rounds +multi_rounds = True +rounds = int(input("How many rounds do you want to play? ")) +current_round = 1 +while multi_rounds: + print(f"Round {current_round} of {rounds}") + user_choice = input("Pick a choice (rock/paper/scissors): ") + if user_choice not in input_choices: + print("Invalid choice, please pick rock, paper, or scissors.") + continue + cpu_choice = random.choice(input_choices) + print(f"Computer picked: {cpu_choice}") + if user_choice == cpu_choice: + print("It's a draw!") + elif (user_choice == "rock" and cpu_choice == "scissors") or \ + (user_choice == "paper" and cpu_choice == "rock") or \ + (user_choice == "scissors" and cpu_choice == "paper"): + print("You win!") + else: + print("Computer wins!") + current_round += 1 + if current_round > rounds: + multi_rounds = False