Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

Python: Can't figure out why my loop skips the right answer

I decided to make a small project to test my skills as I continue to learn Python in my free time.

The game consists of the user guessing the right number that is randomly generated within a certain amount of tries. The user first enters the range of numbers they want to guess from. Then they get their first try at guessing the right number (I have the randomly generated number displayed on purpose to test my code as I continue). I cannot figure out why when I enter the same number as the randomly generated number, I get the error that would pop up when you guess the wrong number. But if I enter that same number after I am prompted to guess for the randomly generated number again, I get a success note prompted to me. I've been trying different variations all day.

import random

print("Guessing Game")
rangeAmount = int(input("From 1 to what number do you want to guess from (Maximum amount is 50)? "))
correctNum = random.randint(1, rangeAmount)
wrongCount = 0
userScore = 0

print("-" * 50)

while rangeAmount:
    if 1 < rangeAmount < 10:
        guesses = 3
        print("Guesses allowed: 3")
        break
    if 1 < rangeAmount < 20:
        guesses = 4
        break
    if 1 < rangeAmount < 30:
        guesses = 5
        break
    if 1 < rangeAmount < 40:
        guesses = 6
        break
    if 1 < rangeAmount < 50:
        guesses = 7
        break

print("Correct number: " + str(correctNum))

print("Guess amount: " + str(guesses))

    print("-" * 50)

userGuess = input("Make a guessing attempt for the correct number: ")

while userScore != 3:
    if wrongCount != guesses:
    if userGuess is correctNum:
        userScore += 1
        print("You got the right answer")
        break
    else:
        wrongCount += 1
        print("Current guess count: {}".format(wrongCount))
        userGuess = int(input("Wrong answer, try again: "))
        if wrongCount == guesses:
            print("Out of guesses, score is : {}".format(userScore))
            userScore -= 1
            break


if userScore == 3:
    print("You won the game!")

Output:

Guessing Game
From 1 to what number do you want to guess from (Maximum amount is 50)? 23
--------------------------------------------------
Correct number: 5
Guess amount: 5
--------------------------------------------------
Make a guessing attempt for the correct number: 5
Current guess count: 1
Wrong answer, try again: 5
You got the right answer

Process finished with exit code 0
question from:https://stackoverflow.com/questions/65623416/python-cant-figure-out-why-my-loop-skips-the-right-answer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, your maximum range is 50, but it is not included in your first while loop (ends at 49), change the last line to <= 50. You can remove the while loop, and change the if statements to if/elifs. Second, your indentation is off in the while userScore != 3: loop, but that could just be a copy/paste error.

And now for the most likely cause of the error,

userGuess = input("Make a guessing attempt for the correct number: ")

is a string, don't forget to make it an int before you compare it to another int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...