Home > Net >  If and else not working, incorrect output
If and else not working, incorrect output

Time:07-24

It should only accept when the input is y or yes case insensitive. If any other else, it should end the program and print "Bye! Good game!".

However, when I run the program and I enter "y" or "yes" or any letter, the program still runs.

Also, I am not sure where to properly enter the casefold() so I placed it twice.

continue_game = input("Try another game: Yes/No: ").casefold()
    
if (continue_game == 'y' or continue_game == "yes").casefold():
    print(continue_game)
    continue

else:
    continue_game != 'y'
    print("Bye! good game!")

CodePudding user response:

Try this:

continue_game = input("Try another game: Yes/No: ").casefold()
# assuming this next part is in a loop of sorts

while <insert condition here>:
    if continue_game in ("y", "yes"):
       print(continue_game)
    else:
       print("Bye! good game!")
       break

In order for break to work, the second part of the code must be in a loop. Also, you don't need the continue clause, unless you were planning on having code after your else block that you didn't want to execute.

CodePudding user response:

You should put your code in an "infinite" while loop, you can achieve that by using "True". Your loop will run until you cut off it with the "break". You should only use the casefold() after the input.

while True:
    continue_game = input("Try another game: Yes/No: ").casefold()
    if (continue_game == 'y' or continue_game == "yes"):
        print(continue_game)
        
    else:
        print("Bye! good game!")
        break

CodePudding user response:

This expression:

(continue_game == 'y' or continue_game == "yes").casefold()

should raise an AttributeError, because the expression (continue_game == 'y' or continue_game == "yes") is a bool, and a bool doesn't have a casefold method.

The idea of casefold() is that you apply it to the two strings you're comparing in order to make the comparison case-sensitive. So you'd use it like:

continue_game = input("Try another game: Yes/No: ").casefold()
    
if continue_game == 'y'.casefold() or continue_game == "yes".casefold():
    ...

Since the strings you're comparing against are hard-coded lowercase strings, though, you don't really need to casefold them. I'd suggest just explicitly lowering the continue_game input:

continue_game = input("Try another game: Yes/No: ").lower()

if continue_game == 'y' or continue_game == "yes":
    ...

or simpler yet:

continue_game = input("Try another game: Yes/No: ").lower()

if continue_game in ('y', 'yes'):
    ...

or even just looking at the first letter (so you accept anything starting with "y", like yes or yeah or yup):

continue_game = input("Try another game: Yes/No: ").lower()

if continue_game.startswith('y'):
    ...

Moving further along in the code -- the problem with your if/else is that (assuming you have this in a loop that doesn't terminate by some other means) a loop will continue if you don't do anything to break it. So the continue is pointless, because the loop was going to continue anyway -- and after you print Bye!, the loop does exactly that.

What you want to do instead is break the loop when you want it to stop:

continue_game = input("Try another game: Yes/No: ").lower()

if continue_game.startswith('y'):
    print(continue_game)
else:
    print("Bye! good game!")
    break

CodePudding user response:

I have this for you, if you like something simple, fast, no loop (loop is not needed in this case except you want to ask for the input again or you can replace print with a call to main function of the game ). This will print(continue_game) when input is yes, YES, YeS, YEs, Y or y. And will print("Bye! good game!") when the input is NO, No, nO, N, n, or any other input beyond "Yes" :)

continue_game = input("Try another game: Yes/No: ")
print(continue_game) if continue_game.lower() in ('y', 'yes') else print("Bye! good game!")
  • Related