Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Guessing game never ending

I made a simple guessing game in Python, since I am new to Python. And I encountered an issue that I cannot seem to fix. I recently added a feature where it asks you if you want to play again, and it plays the game again even if you say no.

Here’s the code for the inputs:

        print("You won the game! The answer was " + str(answer))
        playagain = input("Would you like to play again?(Y/N): ")
        if playagain == "Y" or "y":
            startgame()

I tried it with an else statement but it didn’t change anything

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s the code to my entire game:

from random import randint
def startgame(): 

    guessamount = input("How large do you want the range of numbers to pick be?(Limit is 500): ")
    if int(guessamount) >= int(500):
        guessamount = 500

    answer = randint(int(1), int(guessamount))
    attempts = 5

    while attempts != 0 and attempts != 6:
        guess = input("Guess a number between 0-" + str(guessamount) + ": ")

        if answer == int(guess):
            attempts = 6
        else:
            attempts -= 1

            if int(guess) > int(answer):
                print ("You suck at this game. The answer is lower than " + str(guess))
            else:
                print ("You suck at this game. The answer is higher than " + str(guess))

    if answer == int(guess) and attempts != 5:
        print("You won the game! The answer was " + str(answer))
        playagain = input("Would you like to play again?(Y/N): ")
        if playagain == "Y" or "y":
            startgame()
    else:
        print("You ran out of attempts. The number was " + str(answer))
        playagain = input("Would you like to play again?(Y/N): ")
        if playagain == "Y" or "y":
            startgame()
run_once = 0
while 1:
    if run_once == 0:
        startgame()
        run_once = 1

>Solution :

if playagain == "Y" or "y":
    startgame()

Means »if playagain is "Y" OR "y" is truthy«. Obviously, "y" is never false, therefore always truthy.

In other words, your condition is identical to the following:

if playagain == "Y" or "y" != False:
    startgame()

(actually if playagain == "Y" or bool("y") is True:)

Instead, you need to compare your variable on both sides of the or operator:

if playagain == "Y" or playagain == "y":
    startgame()

or possibly:

if playagain in ("Y", "y"):
    startgame()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading