简体   繁体   中英

How to fix guessing game

The objective is to create a simple program that generates a number between 1 and 100, it will then ask the user to guess this, if they guess outside of the number range it should tell them to guess again, if not it should tell them whether their guess was too high or too low, prompting them to guess again. Once they do guess the correct number it should tell them they've won and the number of tries it took for them to guess it correctly.

Here is what I have so far

import random   

def play_game():
    number = random.randint(1, 100)
    print("Guess a number between 1 and 100 inclusive.")
    count = 1
    while True:
        guess = int(input("Your guess: "))

    if guess > 0 and guess <= 100:
        #the age is valid
        return play_game
    else:
        print("Invalid number.")
        return play_game()

        if guess < number:
            print("Too low.")
        elif guess > number:
            print("Too high.")
        elif guess == number:
            print("You won! You guessed it in " + str(count) + " tries.\n")
            return
        count+=1
play_game()

The issue I'm currently running into is when it checks to see if their guess was between 1-100 instead of moving on to weather or not their number was too how or to low, it stays and loops.

If anyone could help me with this issue and review the code in general I'd appreciate it.

I think the problem is with some indentation and some logical problems in the flow. When you call play_game() from inside the game, it starts a completely different game with different random_number.

A good code that satisfies your condition might look like the following

import random   

def play_game():
    number = random.randint(1, 100)
    print("Guess a number between 1 and 100 inclusive.")
    count = 1
    while True:
        guess = int(input("Your guess: "))
        if guess > 0 and guess <= 100:
            if guess < number:
                print("Too low.")
            elif guess > number:
                print("Too high.")
            elif guess == number:
                print("You won! You guessed it in " + str(count) + " tries.\n")
                return
            count+=1
        else:
            print("Invalid number.")

play_game()

You could re-adjust your code:
1. if no. within range, run your high, low, match checks
2. break if guess matches the no

import random

def play_game():
    number = random.randint(1, 100)
    print("Guess a number between 1 and 100 inclusive.")
    count = 0
    while True:
        count += 1
        guess = int(input("Your guess: "))
        if guess > 0 and guess <= 100:
            #the age is valid
            if guess < number:
                print("Too low.")
            elif guess > number:
                print("Too high.")
            elif guess == number:
                print("You won! You guessed it in " + str(count) + " tries.\n")
                break
        else:
            print("Invalid number, try again")
play_game()

The issue you are running into is because of incorrect indentation. The if-else statements that check whether the number is within the valid range are at the same indentation level as the while loop and thus are not executed within it. Simply indenting should fix the problem. Furthermore, you have called play_game without parenthesis, making it incorrect syntax for a function call. However, rather than checking if the number is greater than 0 and lesser than 100, it would more optimal to check whether number is lesser than 0 or greater than 100, and if that is the case, print invalid number and call play_game(). It would look something like this:

while True:
    if guess < 0  and guess > 100:
        print ("Invalid number.")
        return play_game()

The rest of your code looks good. I've also attached the link on the section of indentations of the Python documentation here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM