0

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
  • I don't see the first issue you claim to be having. It says `Lower`, `Higher`, or `Congrats` depending on the guess. – Selcuk Mar 30 '21 at 00:54
  • The `print("Lower!")` and `print("Higher!")` statements should be switched. If the guessed number is lower than the secret number, `_var_usernumber < _var_secretnumber`, then the guesser needs to guess higher, not lower. – bbnumber2 Mar 30 '21 at 01:00
  • Ah sure! That's why the program always said "Lower!" xD But I have one more problem: I want the user to type 'y' for playing again and 'n' for not playing again. How can I realise this? The code with if _again_var != 'y': guessing = False is wrong - that's what PyCharm says... – PythonLuca Mar 30 '21 at 01:06
  • @PythonLuca Please read my comment above, there is no such issue. It does **not** always say `Lower!`. – Selcuk Mar 30 '21 at 01:41

0 Answers0