|
1 | 1 | """ |
2 | 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. |
| 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 | +
|
7 | 8 | This is done by having the user enter either 'add' or 'subtract' every turn. |
8 | 9 | The main number will be updated to the new value. |
| 10 | +
|
9 | 11 | A player wins if they make 12 to be the main number. |
10 | | -If the end of the list is reached, go back to the first element |
| 12 | +If the end of the list is reached, go back to the first element |
11 | 13 | in the list and keep going until the player wins. |
12 | | -Code the game for Snookle! |
| 14 | +Code the game for Snookle! |
13 | 15 | """ |
14 | 16 |
|
15 | | -nums = [3,1,4,2,6,5,8,10] |
| 17 | +# example values to get you started |
| 18 | +nums = [3, 1, 4, 2, 6, 5, 8, 10] |
16 | 19 | main = 7 |
17 | 20 |
|
18 | | -choice = '' |
19 | | -win = False #The game keeps going until this variable is set to True |
20 | | - |
21 | | -while (not win): |
22 | | - for i in nums: |
23 | | - 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 |
24 | | - choice = input("[add] or [subtract] " + str(i) + "?\n> ") #text is also optional, I just added it to make it more user-friendly |
25 | | - if(choice == 'add'): #The user chooses to add |
26 | | - main += i #update main value |
27 | | - elif(choice == 'subtract'): #The user chooses to subtract |
28 | | - main -= i #update main value |
29 | | - if(main == 12): #If the main number if 12, the user has won! |
30 | | - win = True #This will make the program exit out of the while loop, thus ending the game |
31 | | - break #This takes us out of the for loop and straight to the while statement |
| 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 |
32 | 39 |
|
33 | 40 | print('Congrats you won the game!') |
0 commit comments