0

I was doing beginner python projects as I am a beginner still. I came upon this guessing game project and Wanted to do a Yes or No type of question on whether the user wants to take part in guessing or not. If user enters Yes, they take part, if anything else ("No" or anything the program exits the code)

However I seem to be doing something wrong, when I enter Yes, the program exits the code anyway. What am I doing wrong? Thank you all, Here is the code. I am only posting a part of it, where I most probably get the error.

import random

guess = 0
name = input("Hello what is your name?: ")
num = random.randint(1 , 50)
response = input("Well hello there " + name + "  I have a number between 1-50, Want to play? You have 10 tries")


if response != "Yes" and response != "yes":
     exit()
else:
     while guess <= 10:
        guess += 1
        take = int(input("Guess a number!"))
        if take == num:
            print("You win! You guessed " + str(guess) + " times")
        elif take > num:
            print("Too high!")
        elif take < num:
            print("Thats too low!")
        elif take >= guess:
            print("You lose... The number was "+ num)


Jos
  • 65
  • 7
  • I believe its edited now, sorry – Jos Dec 16 '20 at 15:21
  • Does this answer your question? [Why does \`a == b or c or d\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – Tomerikoo Dec 16 '20 at 15:34

2 Answers2

2
if response != "Yes" or "yes":

equates to this:

if response != "Yes"     # which resolves to False when response is a 'no'

OR

"yes"      # which is a non-empty string, which Python equates to True.

So basically, you code is equivalent to:

if False OR True: 

and thus it always runs the exit() function.

In addition to the items noted above, the if statement should be checking both conditions and thus it should be using and instead of using or, as shown below (HT to @tomerikoo):

What you need is TWO separate tests in the if statement:

if response != "Yes" and response != "yes":

If you believe that you might have other versions of yes answers OR if you think that doing a comparison against a general sequence of terms might be easier to understand, you can also do this test instead:

if response in ['Yes', 'yes', 'y', 'Y', 'YES']:

Debugging:

For folks who are new to programming, in Python, it is sometimes fast and easy to use a simple print() function to rapidly evaluate the current state of a variable, to ensure that it really points at the value you believe it does. I added a print statement below with a comment. It would be beneficial to see what value is associated with response. (Caveat: there are professional tools built into Python and into code editors to help with watching variables and debugging, but this is fast and easy).

import random

guess = 0
name = input("Hello what is your name?: ")
num = random.randint(1 , 50)
response = input("Well hello there " + name + "  I have a number between 1-50, Want to play? You have 10 tries")
print(response)  # checking to see what was actually returned by the 
                 # input() method


if response != "Yes" and response != "yes":
    print(response)  # check to see what the value of response is at
                     # the time of the if statement (i.e. confirm that
                     # it has not changed unexpectedly).
    exit()
else:
     while guess <= 10:
        guess += 1
        take = int(input("Guess a number!"))
        if take == num:
            print("You win! You guessed " + str(guess) + " times")
        elif take > num:
            print("Too high!")
        elif take < num:
            print("Thats too low!")
        elif take >= guess:
            print("You lose... The number was "+ num)
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • Hi, I tried your suggestion but the code still exits when I type Yes or yes in the program. – Jos Dec 16 '20 at 15:40
  • 1
    @Jos Because it should be `if response != ... and ...` or `if response not in ...` – Tomerikoo Dec 16 '20 at 15:55
  • I am sorry but it doesn't seem to work, something is wrong... – Jos Dec 16 '20 at 15:56
  • Please show what exactly you tried. If it's too long, edit it to the question or ask a new one – Tomerikoo Dec 16 '20 at 15:57
  • @Jos see my changes to my answer. Let me know if that helps. – E. Ducateme Dec 16 '20 at 17:26
  • Hi, I tried but it doesn't seem to work, I edited my answer and put the whole code. I hope it helps. I apologize for all of this. – Jos Dec 17 '20 at 08:59
  • @jos please don't feel the need to apologize. We are here to help. I cut and pasted your code, exactly as you have it above and it works as expected. I typed in Yes and yes and it allowed me to guess numbers. I typed in no and it exited the script, as expected. – E. Ducateme Dec 17 '20 at 13:09
  • Hi, did it work as intended? why do you believe I have errors in my code when you copy pasted the same one? – Jos Dec 17 '20 at 16:37
  • @Jos I added a few ideas on debugging the code to my answer by using `print()` to evaluate the current state of a variable. That may shed some light on what is really happening under the hood. – E. Ducateme Dec 18 '20 at 14:52
  • Apparently the whole issue was that when I press space on my keyboard the code exits because it doesn't seem to understand " yes" instead of "yes" – Jos Dec 20 '20 at 15:33
0

In python "non-empty random str" is True and empty string "" is False for conditional usages. So,

if "yes":
  print("foo")

prints 'foo'

if "":
  print("foo")
else:
  print("bar")

prints 'bar'

In your case you want program to exit if response is not "Yes" or not "yes". You have two conditions evaluated in if statement:

  1. response == "Yes" --> False
  2. "yes" --> True

if 1 or 2 --> True --> so exit.

so it should be like that:

if response not in ["Yes", "yes"]:
  exit()
else:
  do_something()
sagmansercan
  • 101
  • 4
  • This didn't really work, sorry I am not sure what I have wrong. – Jos Dec 16 '20 at 15:57
  • can you share your error output (Traceback)? – sagmansercan Dec 16 '20 at 16:08
  • Hi, Sorry, what is exactly the traceback? Because I don't get an error code, The program runs, just runs in the wrong way – Jos Dec 16 '20 at 16:13
  • Sorry, I thought it raised an error. if response is "yes" or "Yes" program should continue and should exit for other responses. This part must work as expected at least. What is the response and what does program do at this moment, may be you can share these ? – sagmansercan Dec 16 '20 at 16:29
  • Thats the problem, the program exits for "Yes" and also exits for anything else I type other than yes or Yes. – Jos Dec 16 '20 at 16:31
  • if you are using `if response not in ["Yes", "yes"]` as in the answer ,it is certain that program will only exist for other values. It is better to print(response) before if/else conditions. – sagmansercan Dec 16 '20 at 16:37