diff --git a/1_beginner/chapter4/practice/temperature.py b/1_beginner/chapter4/practice/temperature.py new file mode 100644 index 00000000..0734720b --- /dev/null +++ b/1_beginner/chapter4/practice/temperature.py @@ -0,0 +1,14 @@ +""" +Hoppity the Rabbit wrote some code, but it doesn't run correctly and he needs your help! +The following program is supposed to print a string based on a numerical input of the temperature. +See if you can fix it for him! +""" +#I added some comments to aid you. Good luck! - Hoppity +temp = int(input("Enter temperature: ")) #possible input range is 0-100 + +if(temp < 100): #60-100 is hot + print('hot') +if(temp <= 60): #30-59 is normal + print('normal') +if(temp < 30): #0-29 is cold + print('cold') diff --git a/1_beginner/chapter4/solutions/menu.py b/1_beginner/chapter4/solutions/menu.py new file mode 100644 index 00000000..ce2bfad1 --- /dev/null +++ b/1_beginner/chapter4/solutions/menu.py @@ -0,0 +1,9 @@ +food = input("Enter food order: ") +drink = input("Enter drink order: ") +price = 1000000000 +if(food == 'french toast' and drink == 'coffee'): + price = price - 1 +elif(food == 'chicken soup' or drink == 'apple juice'): + price = price + 1 + +print('The total price is $' + str(price)) diff --git a/1_beginner/chapter4/solutions/monthandday.py b/1_beginner/chapter4/solutions/monthandday.py new file mode 100644 index 00000000..cbd70e95 --- /dev/null +++ b/1_beginner/chapter4/solutions/monthandday.py @@ -0,0 +1,8 @@ +month = input('Enter the month: ') +day = input('Enter the day: ') + +if(month == 10 and day == 31): + print('Boo!') +elif(month == 4 and day == 1): + print('April fools!') + diff --git a/1_beginner/chapter4/solutions/square.py b/1_beginner/chapter4/solutions/square.py new file mode 100644 index 00000000..532a6246 --- /dev/null +++ b/1_beginner/chapter4/solutions/square.py @@ -0,0 +1,5 @@ +x = int(input("Enter a number: ")) +if(x % 2 == 0): + print(x**2) +else: + print(x) diff --git a/1_beginner/chapter4/solutions/temperature.py b/1_beginner/chapter4/solutions/temperature.py new file mode 100644 index 00000000..b6333ec4 --- /dev/null +++ b/1_beginner/chapter4/solutions/temperature.py @@ -0,0 +1,10 @@ +temp = int(input("Enter temperature: ")) #possible input range is 0-100 + +if(60 <= temp <= 100): #60-100 is hot + print('hot') +if(30 <= temp < 60): #30-59 is normal + print('normal') +if(0 <= temp < 30): #0-29 is cold + print('cold') + +#alternatively, you can also use elif diff --git a/1_beginner/chapter4/solutions/walk.py b/1_beginner/chapter4/solutions/walk.py new file mode 100644 index 00000000..cf8146f7 --- /dev/null +++ b/1_beginner/chapter4/solutions/walk.py @@ -0,0 +1,20 @@ +first_hour = int(input('Enter the hour of the time of the first walk: ')) +first_minute = int(input('Enter the minute of the time of the first walk: ')) + +second_hour = first_hour + 1 + 6 #add first_hour + 1 is when the first walk ends, 6 hours later the second walk should start +second_minute = first_minute + +current_hour = int(input('Enter the current hour: ')) +current_minute = int(input('Enter the current minute: ')) + +if(current_hour > second_hour): + print('Late') +elif(current_hour < second_hour): + print('Early') +else: #current_hour == second_hour + if(current_minute > second_minute): + print('Late') + elif(current_minute < second_minute): + print('Early') + else: #current_minute == second_minute + print('Now') diff --git a/1_beginner/chapter5/practice/virtual_pet.py b/1_beginner/chapter5/practice/virtual_pet.py new file mode 100644 index 00000000..04a7442f --- /dev/null +++ b/1_beginner/chapter5/practice/virtual_pet.py @@ -0,0 +1,14 @@ +""" +Virtual Pet +Code a virtual pet! +First, create three variables: name, hunger, and happiness. +Initialize hunger to 6 and happiness to 0, and set name to whatever you like. (e.g. 'Otto') +Make a program that satisfies the following: + Print "[pet name] is hungry" when hunger is above 5, where [pet name] is the name variable. + Print "[pet name] wants more attention" when happiness is below 5. + Print "[pet name] feels happy" when hunger is equal to or less than 5, and happiness is equal to or greater than 5. + Takes input from the user. + If the user enters 'feed', decrease hunger by 2. Increase by 1 otherwise. + If the user enters 'pet', increase happiness by 2. Decrease by 1 otherwise. + If the user enters 'quit', end the program. +""" diff --git a/1_beginner/chapter5/solutions/virtual_pet.py b/1_beginner/chapter5/solutions/virtual_pet.py new file mode 100644 index 00000000..13ea0c25 --- /dev/null +++ b/1_beginner/chapter5/solutions/virtual_pet.py @@ -0,0 +1,36 @@ +""" +Virtual Pet +Code a virtual pet! +First, create three variables: name, hunger, and happiness. +Initialize hunger to 6 and happiness to 0, and set name to whatever you like. (e.g. 'Otto') +Make a program that satisfies the following: + Print "[pet name] is hungry" when hunger is above 5, where [pet name] is the name variable. + Print "[pet name] wants more attention" when happiness is below 5. + Print "[pet name] feels happy" when hunger is equal to or less than 5, and happiness is equal to or greater than 5. + Takes input from the user. + If the user enters 'feed', decrease hunger by 2. Increase by 1 otherwise. + If the user enters 'pet', increase happiness by 2. Decrease by 1 otherwise. + If the user enters 'quit', end the program. +""" + +name = 'Otto' +hunger = 6 +happiness = 0 + +cmd = input('> ') +while(cmd != 'quit'): #Exits loop when user quits + if(cmd == 'feed'): + hunger -= 2 #Otto is fed, hunger decreases + happiness -= 1 #Otto is not pet, happiness decreases + elif(cmd == 'pet'): + happiness += 2 #Otto is pet, happiness increases + hunger += 1 #Otto is not fed, hunger increases + if(hunger > 5): #Otto is not fed enough + print(name + ' is hungry') + if(happiness < 5): #Otto is not pet enough + print(name + ' wants more attention') + elif(hunger <= 5 and happiness >= 5): #Otto is satisfied! + print(name + ' feels happy') + cmd = input('> ') #Keep taking user input until user quits + +# Feel free to customize your virtual pet by changing the how much the hunger and happiness variables increase and decrease, or add more actions! diff --git a/1_beginner/chapter6/examples/too_long b/1_beginner/chapter6/examples/too_long new file mode 100644 index 00000000..9a34a81e --- /dev/null +++ b/1_beginner/chapter6/examples/too_long @@ -0,0 +1,5 @@ +""" +Print and remove all elements in a given list with length greater than 4. +""" +#example list to help you test your code +the_list = ['dragon', 'cab', 'science', 'dove', 'lime', 'river', 'pop'] diff --git a/1_beginner/chapter6/integer_info.py b/1_beginner/chapter6/integer_info.py new file mode 100644 index 00000000..69e17e10 --- /dev/null +++ b/1_beginner/chapter6/integer_info.py @@ -0,0 +1,12 @@ +""" +Integer Info +Create a program that takes an integer as input and creates a list with the following elements: + The number of digits + The last digit + A 'True' boolean value if the number is even, 'False' if odd +Print the list. +Some examples are given to help check your work. +""" + +#Example 1: The input 123456 should print [6, 6, True] +#Example 2: The input 101202303 should print [9, 3, False] diff --git a/1_beginner/chapter6/integer_info_sol.py b/1_beginner/chapter6/integer_info_sol.py new file mode 100644 index 00000000..fe6c67f6 --- /dev/null +++ b/1_beginner/chapter6/integer_info_sol.py @@ -0,0 +1,14 @@ +""" +Integer Info +Create a program that takes an integer as input and creates a list with the following elements: + The number of digits + The last digit + A 'True' boolean value if the number is even, 'False' if odd +Print the list. +Some examples are given to help check your work. +""" +#Example 1: The input 123456 should print [6, 6, True] +#Example 2: The input 101202303 should print [9, 3, False] + +num = int(input()) #convert string input to int +print([len(str(num)), num % 10, (num % 2 == 0)]) #taking the modulo of 10 of any number will return its last digit diff --git a/1_beginner/chapter6/monty_hall.py b/1_beginner/chapter6/monty_hall.py new file mode 100644 index 00000000..a2b79c85 --- /dev/null +++ b/1_beginner/chapter6/monty_hall.py @@ -0,0 +1,28 @@ +""" +Monty Hall +Code the classic Monty Hall problem! +The following is a description of the Monty Hall Problem: + There are three closed doors, 2 have goats behind them, only one has a car behind it. + You don't know which door has what. The goal is to pick the door that has the car behind it. + After you make a choice, Monty (the host) opens a door that you did not choose, revealing a goat. + You are then asked whether you want to change your choice to the other door. + The door you chose is more likely to have a car if you switch to the other door, apparantly! +More step-by-step instructions are commented below. +Read more about the Monty Hall Problem here: (https://betterexplained.com/articles/understanding-the-monty-hall-problem/) +""" +#Make the Monty Hall game repeat 6 times. + +#Make a list that represents the three closed doors, 'G' for the doors that have a goat, 'C' for the door that has a car. +#This step has already been done for you. +import random +monty_hall = ['G', 'G', 'C'] + +random.shuffle(monty_hall) #The shuffle function from the random module randomizes the doors every loop + +#The user enters their 1st choice. + +#A door that has a goat is revealed, cannot be the one user chose + +#The user is prompted to choose again + +#The prize behind the user's ultimate choice is revealed! diff --git a/1_beginner/chapter6/monty_hall_sol.py b/1_beginner/chapter6/monty_hall_sol.py new file mode 100644 index 00000000..07ba7902 --- /dev/null +++ b/1_beginner/chapter6/monty_hall_sol.py @@ -0,0 +1,35 @@ +""" +Monty Hall +Code the classic Monty Hall problem! +The following is a description of the Monty Hall Problem: + There are three closed doors, 2 have goats behind them, only one has a car behind it. + You don't know which door has what. The goal is to pick the door that has the car behind it. + After you make a choice, Monty (the host) opens a door that you did not choose, revealing a goat. + You are then asked whether you want to change your choice to the other door. + The door you chose is more likely to have a car if you switch to the other door, apparantly! +More step-by-step instructions are commented below. +Read more about the Monty Hall Problem here: (https://betterexplained.com/articles/understanding-the-monty-hall-problem/) +""" +#Make a list that represents the three closed doors, 'G' for the doors that have a goat, 'C' for the door that has a car. +import random +monty_hall = ['G', 'G', 'C'] + +#Make the Monty Hall game repeat 6 times. +for i in range(6): + random.shuffle(monty_hall) + #The user enters their 1st choice + choice = input("Choose from doors 1, 2, and 3... : ") + #A door that has a goat is revealed, cannot be the one user chose + reveal = list(monty_hall) #makes a duplicate list to avoid messing up the original + reveal[int(choice) - 1] = '-' #removes user's choice so that it won't be opened, but keeps in the element to not mess up the indices + print("Monty opens door " + str(reveal.index('G') + 1) + " to reveal a goat!") + #The user is prompted to choose again + choice = input("Re-enter your choice. You have the option to switch to the other door.\n> ") + print("You got... ") + #The prize behind the user's ultimate choice is revealed! + if(monty_hall[int(choice) - 1] == 'C'): + print("a car! Congratulations!") + else: + print("a goat! Better luck next time!") + + input("Type anything to play again\n> ") diff --git a/1_beginner/chapter6/snookle_game.py b/1_beginner/chapter6/snookle_game.py new file mode 100644 index 00000000..a0e78cad --- /dev/null +++ b/1_beginner/chapter6/snookle_game.py @@ -0,0 +1,17 @@ +""" +Snookle Game +Snookle the sheep wants to play a game. +Given a list of positive integers and a main number, the player iterates +through each element in the list and chooses to either add it or +to subtract it from the current main number. +This is done by having the user enter either 'add' or 'subtract' every turn. +The main number will be updated to the new value. +A player wins if they make 12 to be the main number. +If the end of the list is reached, go back to the first element +in the list and keep going until the player wins. +Code the game for Snookle! +""" + +#example values to get you started +nums = [3,1,4,2,6,5,8,10] +main = 7 diff --git a/1_beginner/chapter6/snookle_game_sol.py b/1_beginner/chapter6/snookle_game_sol.py new file mode 100644 index 00000000..0a6f440c --- /dev/null +++ b/1_beginner/chapter6/snookle_game_sol.py @@ -0,0 +1,33 @@ +""" +Snookle Game +Snookle the sheep wants to play a game. +Given a list of positive integers and a main number, the player iterates +through each element in the list and chooses to either add it or +to subtract it from the current main number. +This is done by having the user enter either 'add' or 'subtract' every turn. +The main number will be updated to the new value. +A player wins if they make 12 to be the main number. +If the end of the list is reached, go back to the first element +in the list and keep going until the player wins. +Code the game for Snookle! +""" + +nums = [3,1,4,2,6,5,8,10] +main = 7 + +choice = '' +win = False #The game keeps going until this variable is set to True + +while (not win): + for i in nums: + print('main number is currently ' + str(main)) #this is optional, but it's easier for the players since they won't have to keep track of the math + choice = input("[add] or [subtract] " + str(i) + "?\n> ") #text is also optional, I just added it to make it more user-friendly + if(choice == 'add'): #The user chooses to add + main += i #update main value + elif(choice == 'subtract'): #The user chooses to subtract + main -= i #update main value + if(main == 12): #If the main number if 12, the user has won! + win = True #This will make the program exit out of the while loop, thus ending the game + break #This takes us out of the for loop and straight to the while statement + +print('Congrats you won the game!') diff --git a/1_beginner/chapter6/too_long.py b/1_beginner/chapter6/too_long.py new file mode 100644 index 00000000..d458adda --- /dev/null +++ b/1_beginner/chapter6/too_long.py @@ -0,0 +1,6 @@ +""" +Too Long +Print and remove all elements in a given list with length greater than 4. +""" +#list to help you test your code +the_list = ['dragon', 'cab', 'science', 'dove', 'lime', 'river', 'pop'] diff --git a/1_beginner/chapter6/too_long_sol.py b/1_beginner/chapter6/too_long_sol.py new file mode 100644 index 00000000..b5cdb31e --- /dev/null +++ b/1_beginner/chapter6/too_long_sol.py @@ -0,0 +1,10 @@ +""" +Too Long +Print and remove all elements in a given list with length greater than 4. +""" +the_list = ['dragon', 'cab', 'science', 'dove', 'lime', 'river', 'pop'] + +for x in the_list: #iterates through every element in the_list + if(len(x) > 4): #if length is greater than 4 + print(x) #print it + the_list.remove(x)