Guessing game never ending

Question:

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

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
Asked By: Possible Panda

||

Answers:

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()
Answered By: knittl
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 playagain == "y":
            play_again = True
        else:
            play_again = False
        return play_again
    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 playagain == "y":
            play_again = True
        else:
            play_again = False
        return play_again

play_again = True
while play_again:
    play_again = startgame()
Answered By: Stef Renneboog
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.