Jos
Jos

Reputation: 67

How to separate if statements and exit code to execute the program properly?

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)


Upvotes: 0

Views: 140

Answers (2)

E. Ducateme
E. Ducateme

Reputation: 4248

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)

Upvotes: 2

sagmansercan
sagmansercan

Reputation: 101

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()

Upvotes: 0

Related Questions