Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
17a12a9
Create temperature.py
BitPupper Jun 26, 2020
ba1fa01
Create square.py
BitPupper Jun 26, 2020
dd4fbaf
Create menu.py
BitPupper Jun 26, 2020
3a38417
Create monthandday.py
BitPupper Jun 26, 2020
7277ebf
Create walk.py
BitPupper Jun 26, 2020
e480f56
Create temperature.py
BitPupper Jun 26, 2020
0b80df4
Create too_long
BitPupper Jul 1, 2020
8352924
Create too_long_sol
BitPupper Jul 1, 2020
949c66a
Create virtual_pet
BitPupper Jul 1, 2020
6048c9f
Create virtual_pet_sol
BitPupper Jul 1, 2020
ba10022
Create too_long
BitPupper Jul 1, 2020
186451b
Create integer_info
BitPupper Jul 1, 2020
be7f4a9
Create integer_info_sol
BitPupper Jul 1, 2020
e2c0bf3
Create snookle_game
BitPupper Jul 1, 2020
ccf512c
Create snookle_game_sol
BitPupper Jul 1, 2020
ac88ba3
Create monty_hall
BitPupper Jul 1, 2020
e873076
Create monty_hall_sol
BitPupper Jul 1, 2020
e743e27
Create virtual_pet
BitPupper Jul 1, 2020
e80ec59
Create virtual_pet
BitPupper Jul 1, 2020
9974024
Rename virtual_pet to virtual_pet.py
BitPupper Jul 1, 2020
46e2d61
Rename virtual_pet to virtual_pet.py
BitPupper Jul 1, 2020
6401be9
Rename integer_info to integer_info.py
BitPupper Jul 1, 2020
0ded7f6
Rename integer_info_sol to integer_info_sol.py
BitPupper Jul 1, 2020
a95a541
Rename monty_hall to monty_hall.py
BitPupper Jul 1, 2020
2755a33
Rename monty_hall_sol to monty_hall_sol.py
BitPupper Jul 1, 2020
4c62bf6
Rename snookle_game to snookle_game.py
BitPupper Jul 1, 2020
ffbcfd0
Rename snookle_game_sol to snookle_game_sol.py
BitPupper Jul 1, 2020
2340913
Rename too_long to too_long.py
BitPupper Jul 1, 2020
880e3b2
Rename too_long_sol to too_long_sol.py
BitPupper Jul 1, 2020
c477be8
Update integer_info.py
BitPupper Jul 1, 2020
0087557
Update integer_info_sol.py
BitPupper Jul 1, 2020
5eef74c
Update monty_hall.py
BitPupper Jul 1, 2020
60bc26a
Update monty_hall_sol.py
BitPupper Jul 1, 2020
e6599ff
Update snookle_game.py
BitPupper Jul 1, 2020
64fbafa
Update snookle_game_sol.py
BitPupper Jul 1, 2020
c972c5e
Update too_long.py
BitPupper Jul 1, 2020
03b6faa
Update too_long_sol.py
BitPupper Jul 1, 2020
9d31bcc
Update virtual_pet
BitPupper Jul 1, 2020
31ddc7a
Delete virtual_pet
BitPupper Jul 1, 2020
e5dba87
Delete virtual_pet_sol
BitPupper Jul 1, 2020
5f9af35
Update virtual_pet.py
BitPupper Jul 1, 2020
860f9c5
Update virtual_pet.py
BitPupper Jul 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 1_beginner/chapter4/practice/temperature.py
Original file line number Diff line number Diff line change
@@ -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')
9 changes: 9 additions & 0 deletions 1_beginner/chapter4/solutions/menu.py
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 8 additions & 0 deletions 1_beginner/chapter4/solutions/monthandday.py
Original file line number Diff line number Diff line change
@@ -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!')

5 changes: 5 additions & 0 deletions 1_beginner/chapter4/solutions/square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = int(input("Enter a number: "))
if(x % 2 == 0):
print(x**2)
else:
print(x)
10 changes: 10 additions & 0 deletions 1_beginner/chapter4/solutions/temperature.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions 1_beginner/chapter4/solutions/walk.py
Original file line number Diff line number Diff line change
@@ -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')
14 changes: 14 additions & 0 deletions 1_beginner/chapter5/practice/virtual_pet.py
Original file line number Diff line number Diff line change
@@ -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.
"""
36 changes: 36 additions & 0 deletions 1_beginner/chapter5/solutions/virtual_pet.py
Original file line number Diff line number Diff line change
@@ -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!
5 changes: 5 additions & 0 deletions 1_beginner/chapter6/examples/too_long
Original file line number Diff line number Diff line change
@@ -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']
12 changes: 12 additions & 0 deletions 1_beginner/chapter6/integer_info.py
Original file line number Diff line number Diff line change
@@ -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]
14 changes: 14 additions & 0 deletions 1_beginner/chapter6/integer_info_sol.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions 1_beginner/chapter6/monty_hall.py
Original file line number Diff line number Diff line change
@@ -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!
35 changes: 35 additions & 0 deletions 1_beginner/chapter6/monty_hall_sol.py
Original file line number Diff line number Diff line change
@@ -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> ")
17 changes: 17 additions & 0 deletions 1_beginner/chapter6/snookle_game.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions 1_beginner/chapter6/snookle_game_sol.py
Original file line number Diff line number Diff line change
@@ -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!')
6 changes: 6 additions & 0 deletions 1_beginner/chapter6/too_long.py
Original file line number Diff line number Diff line change
@@ -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']
10 changes: 10 additions & 0 deletions 1_beginner/chapter6/too_long_sol.py
Original file line number Diff line number Diff line change
@@ -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)