here is the game so far
- Code: Select all
import random
print("Try to get as close to 21 as you can without going over")
a = random.randint(0, 12)
b = random.randint(0, 12)
print("Your current total is ", a + b)
total = a + b
hit = input("Hit? y or n")
while total < 21:
if hit == 'y':
c = random.randint(0, 12)
total2 = total + c
print("your new total is ", total2)
break
if 21 == total:
print("you won!")
if 21 < total:
print("you bust.")
if hit == 'n':
print("your total is ", total)
What i want to happen is
1. take input "y" or "n"
2a. if input "y" then give add another number. repeat until input "n" or the number(the second total variable(i changed it so that the while loop should recognize it)) exceeds 21 or player reaches 21 exactly.
2b. if input "n" print the total.
Here are the problems
1. nothing happens upon inputing "n". when i ctrl c i get this
- Code: Select all
^CTraceback (most recent call last):
File "21.py", line 8, in <module>
while total < 21:
KeyboardInterrupt
2a. If i keep the "break" then it only prints once. i want it to give the option of adding another number. i would think it would do this because it is in a while loop.
2b. if i get rid of the break it just keeps printing.
3. in number 2a sometimes when repeating the number will exceed 21 yet it won't exit the loop. why is this?
Thank you for your help!




