Skip to content

Commit 689f0b0

Browse files
committed
Merge PR #15 local changes with BitPupper-patch-2
2 parents e215bd9 + 2be2190 commit 689f0b0

File tree

10 files changed

+288
-0
lines changed

10 files changed

+288
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Virtual Pet
3+
Code a virtual pet!
4+
First, create three variables: name, hunger, and happiness.
5+
Initialize hunger to 6 and happiness to 0, and set name
6+
to whatever you like. (e.g. 'Otto')
7+
8+
Your program should continuously take input from the user:
9+
If the user enters 'feed', decrease hunger by 2. Increase by 1 otherwise.
10+
If the user enters 'pet', increase happiness by 2. Decrease by 1 otherwise.
11+
If the user enters 'quit', end the program.
12+
13+
Your program should also print the status of your pet each time
14+
it prompts the user to enter a command:
15+
Print "[pet name] is hungry" when hunger is above 5, where [pet name]
16+
is the name variable.
17+
Print "[pet name] wants more attention" when happiness is below 5.
18+
Print "[pet name] feels happy" when hunger is equal to or less than 5,
19+
and happiness is equal to or greater than 5.
20+
21+
Feel free to customize your virtual pet by changing the how much the hunger
22+
and happiness variables increase and decrease, or add more actions!
23+
"""
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Virtual Pet
3+
Code a virtual pet!
4+
First, create three variables: name, hunger, and happiness.
5+
Initialize hunger to 6 and happiness to 0, and set name
6+
to whatever you like. (e.g. 'Otto')
7+
8+
Your program should continuously take input from the user:
9+
If the user enters 'feed', decrease hunger by 2. Increase by 1 otherwise.
10+
If the user enters 'pet', increase happiness by 2. Decrease by 1 otherwise.
11+
If the user enters 'quit', end the program.
12+
13+
Your program should also print the status of your pet each time
14+
it prompts the user to enter a command:
15+
Print "[pet name] is hungry" when hunger is above 5, where [pet name]
16+
is the name variable.
17+
Print "[pet name] wants more attention" when happiness is below 5.
18+
Print "[pet name] feels happy" when hunger is equal to or less than 5,
19+
and happiness is equal to or greater than 5.
20+
21+
Feel free to customize your virtual pet by changing the how much the hunger
22+
and happiness variables increase and decrease, or add more actions!
23+
"""
24+
25+
name = 'Otto'
26+
hunger = 6
27+
happiness = 0
28+
29+
command = input('> ')
30+
while command != 'quit': # Exits loop when user quits
31+
# change Otto's hunger or happiness based on user command
32+
if command == 'feed':
33+
hunger -= 2 # Otto is fed, hunger decreases
34+
happiness -= 1 # Otto is not pet, happiness decreases
35+
elif command == 'pet':
36+
happiness += 2 # Otto is pet, happiness increases
37+
hunger += 1 # Otto is not fed, hunger increases
38+
39+
# display Otto's status
40+
if hunger > 5: # Otto is not fed enough
41+
print(name + ' is hungry')
42+
if happiness < 5: # Otto is not pet enough
43+
print(name + ' wants more attention')
44+
elif hunger <= 5 and happiness >= 5: # Otto is satisfied!
45+
print(name + ' feels happy')
46+
47+
command = input('> ') # Keep taking user input until user quits
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Integer Info
3+
Create a program that takes an integer as input and
4+
creates a list with the following elements:
5+
The number of digits
6+
The last digit
7+
A 'True' boolean value if the number is even, 'False' if odd
8+
Print the list.
9+
Some examples are given to help check your work.
10+
"""
11+
12+
# Example 1: The input 123456 should print [6, 6, True]
13+
# Example 2: The input 101202303 should print [9, 3, False]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Monty Hall
3+
Code the classic Monty Hall problem!
4+
5+
The following is a description of the Monty Hall Problem:
6+
There are three closed doors, 2 have goats behind them,
7+
only one has a car behind it.
8+
You don't know which door has what.
9+
The goal is to pick the door that has the car behind it.
10+
After you make a choice, Monty (the host) opens a door
11+
that you did not choose, revealing a goat.
12+
You are then asked whether you want to
13+
change your choice to the other door.
14+
The door you chose is more likely to have a car
15+
if you switch to the other door, apparently!
16+
17+
More step-by-step instructions (pseudocode) are commented below.
18+
19+
Read more about the Monty Hall Problem here:
20+
https://betterexplained.com/articles/understanding-the-monty-hall-problem/
21+
"""
22+
23+
# Make a list that represents the three closed doors,
24+
# 'G' for the doors that have a goat, 'C' for the door that has a car.
25+
# This step has already been done for you.
26+
import random
27+
doors = ['G', 'G', 'C']
28+
29+
# Make the Monty Hall game repeat 6 times.
30+
# (All of the following actions should be in your loop.)
31+
32+
# The shuffle function from the random module randomizes the doors every loop
33+
random.shuffle(doors)
34+
35+
# The user enters their 1st choice.
36+
37+
# A door that has a goat is revealed, cannot be the one user chose
38+
39+
# The user is prompted to choose again
40+
41+
# The prize behind the user's ultimate choice is revealed!
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Snookle Game
3+
Snookle the sheep wants to play a game.
4+
Given a list of positive integers and a main number, the player iterates
5+
through each element in the list and chooses to either add it or
6+
to subtract it from the current main number.
7+
8+
This is done by having the user enter either 'add' or 'subtract' every turn.
9+
The main number will be updated to the new value.
10+
11+
A player wins if they make 12 to be the main number.
12+
If the end of the list is reached, go back to the first element
13+
in the list and keep going until the player wins.
14+
Code the game for Snookle!
15+
"""
16+
17+
# example values to get you started
18+
nums = [3, 1, 4, 2, 6, 5, 8, 10]
19+
main = 7
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Too Long
3+
Print and remove all elements with length greater than 4 in a given list.
4+
"""
5+
# list to help you test your code
6+
the_list = ['dragon', 'cab', 'science', 'dove', 'lime', 'river', 'pop']
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Integer Info
3+
Create a program that takes an integer as input and
4+
creates a list with the following elements:
5+
The number of digits
6+
The last digit
7+
A 'True' boolean value if the number is even, 'False' if odd
8+
Print the list.
9+
Some examples are given to help check your work.
10+
"""
11+
12+
# Example 1: The input 123456 should print [6, 6, True]
13+
# Example 2: The input 101202303 should print [9, 3, False]
14+
15+
num = int(input("Enter an integer: ")) # convert string input to int
16+
info = [
17+
len(str(num)),
18+
num % 10, # mod 10 of any number will return its last digit
19+
num % 2 == 0
20+
]
21+
print(info)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Monty Hall
3+
Code the classic Monty Hall problem!
4+
5+
The following is a description of the Monty Hall Problem:
6+
There are three closed doors, 2 have goats behind them,
7+
only one has a car behind it.
8+
You don't know which door has what.
9+
The goal is to pick the door that has the car behind it.
10+
After you make a choice, Monty (the host) opens a door
11+
that you did not choose, revealing a goat.
12+
You are then asked whether you want to
13+
change your choice to the other door.
14+
The door you chose is more likely to have a car
15+
if you switch to the other door, apparently!
16+
17+
More step-by-step instructions (pseudocode) are commented below.
18+
19+
Read more about the Monty Hall Problem here:
20+
https://betterexplained.com/articles/understanding-the-monty-hall-problem/
21+
"""
22+
23+
# Make a list that represents the three closed doors,
24+
# 'G' for the doors that have a goat, 'C' for the door that has a car.
25+
import random
26+
doors = ['G', 'G', 'C']
27+
28+
# Make the Monty Hall game repeat 6 times.
29+
for i in range(6):
30+
# Randomize the doors
31+
random.shuffle(doors)
32+
33+
# reset the doors left
34+
doors_left = [1, 2, 3]
35+
36+
# The user enters their 1st choice
37+
print("A new Monty Hall game has begun!")
38+
choice = int(input("Choose from doors 1, 2, or 3...\n> "))
39+
doors_left.remove(choice)
40+
41+
# A door that has a goat is revealed, cannot be the one user chose
42+
# makes a duplicate list to avoid messing up the original
43+
reveal_doors = doors.copy()
44+
45+
# removes user's choice so that it won't be opened,
46+
# but keeps in the element to not mess up the indices
47+
reveal_doors[choice - 1] = '-'
48+
goat_door = reveal_doors.index('G') + 1
49+
doors_left.remove(goat_door)
50+
print("Monty opens door", goat_door, "to reveal a goat!")
51+
52+
# The user is prompted to choose again
53+
print("With this new information, do you want to switch doors?")
54+
print("Your first choice was Door", choice)
55+
print("If you switch, you will be opening Door", doors_left[0])
56+
print("Enter 'y' to switch, or 'n' to keep your first choice.")
57+
switch = input("> ")
58+
59+
if switch == 'y':
60+
choice = doors_left[0]
61+
62+
# The prize behind the user's ultimate choice is revealed!
63+
if doors[choice - 1] == 'C':
64+
print("You got... a car! Congratulations!")
65+
else:
66+
print("You got... a goat! Better luck next time!")
67+
68+
print()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Snookle Game
3+
Snookle the sheep wants to play a game.
4+
Given a list of positive integers and a main number, the player iterates
5+
through each element in the list and chooses to either add it or
6+
to subtract it from the current main number.
7+
8+
This is done by having the user enter either 'add' or 'subtract' every turn.
9+
The main number will be updated to the new value.
10+
11+
A player wins if they make 12 to be the main number.
12+
If the end of the list is reached, go back to the first element
13+
in the list and keep going until the player wins.
14+
Code the game for Snookle!
15+
"""
16+
17+
# example values to get you started
18+
nums = [3, 1, 4, 2, 6, 5, 8, 10]
19+
main = 7
20+
21+
win = False # The game keeps going until this variable is set to True
22+
23+
while not win:
24+
for num in nums:
25+
# prompt user to add or subtract current num
26+
print('main number is currently ' + str(main))
27+
choice = input("[add] or [subtract] " + str(num) + "?\n> ")
28+
29+
# update main value based on choice
30+
if choice == 'add':
31+
main += num
32+
elif choice == 'subtract':
33+
main -= num
34+
35+
# If the main number if 12, the user has won!
36+
if main == 12:
37+
win = True # Exit while loop and end game
38+
break # Exit for loop
39+
40+
print('Congrats you won the game!')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Too Long
3+
Print and remove all elements with length greater than 4 in a given list.
4+
"""
5+
the_list = ['dragon', 'cab', 'science', 'dove', 'lime', 'river', 'pop']
6+
7+
for x in the_list: # iterates through every element in the_list
8+
if len(x) > 4: # if element length is greater than 4
9+
print(x) # prints element
10+
the_list.remove(x) # removes element

0 commit comments

Comments
 (0)