Modifying AskToPlayAgain()

Hello Forum,

I’ve tried modifying the AskToPlayAgain() function to not accept answers besides “y, Y, n, or N”. I thought I had it figured but it appears that I am missing something. You can see my current code below.

When I play the game and reach the “Do you want to play again (Y/N)” section, no matter what I enter, it simply asks me again if I want to play again.

I suspect my mistake is in the “Response != “y”” portion. Can you not have a variable like “Response” equal a character? As well, in the “return” line, “0” returns “false” to a boolean and “1” returns “true” no?

Cheers.

There is indeed a flaw in your logic… The logical OR ( || ) means: if any of the conditions is true, the statement is true.
The while loop will only exit if the statement is false, so that means if all your tests joined by || are false. Which can never happen: the response cannot simultaneously be equal to “y”, “Y”, “n”, and “N”.
Try replacing these || with AND (&&)…

1 Like

Thanks a lot, @Itizir! That fixed it.

Cheers.

Sure thing!

P.S. Perhaps you got confused because you haven’t thought about this:
!( A || B ) is equivalent to ( !A && !B ), and similarly !( A && B ) is the same as ( !A || !B )…