I want to make a number guessing game. But there are two errors:
No matter what number I type in, program always says: "Lower!" Secondary I want the user to type in if he wants to play again when he wins.
So like: do the while
loop until the user wins and types in 'y'. Then do it again. If the user wins and types 'n', I want to print: "Thanks for playing! Press Enter to exit".
How can I realise this?
import random
print("Welcome to the number guessing game!")
manual_yes_no = input("Do you want to read the manual? 'y' for yes, 'n' for playing: ")
if manual_yes_no == 'y':
print("""------------------------------------------------------
----------------------MANUAL--------------------------
------------------------------------------------------
A number between 1 and 10 is generated. You have to
guess the number by typing it. The program says if
your number is lower or higher than the generated
number. Have fun!
------------------------------------------------------
------------------------------------------------------
------------------------------------------------------""")
def guessing():
_var_secretnumber = random.randint(1, 10)
_var_usernumber = 0
_again_var = 'j'
while _var_usernumber != _var_secretnumber:
try:
_var_usernumber = int(input("Your number: "))
if _var_usernumber < _var_secretnumber:
print("Lower!")
if _var_usernumber > _var_secretnumber:
print("Higher!")
if _var_usernumber == _var_secretnumber:
print("Congrats! You won.")
_again_var = input("Play again? Type 'y' for yes or 'n' for no: ")
except ValueError:
print("Wrong data type! Try a new number!")
if _again_var != 'y':
guessing = False
try:
while True:
guessing()
except KeyboardInterrupt:
pass