Party Rock
Party Rock

Reputation: 29

Program is stopping even though conditions haven't been met

It prints "that wasn't an option" When I press Y or N even though I have it

I have it set as != (not equal to)

if c.upper() != 'N' or "Y": 
    print ("that wasn't an option")
if c.upper() != 'N' or "Y": 
    sys.exit()

Whole code

import sys
c = input('do you like piza? Y or N:')
if c.upper() != 'N' or "Y": print ("that wasn't an option")
if c.upper() != 'N' or "Y": sys.exit()
if c.upper() == 'Y':
print ('Me To') 
if c.upper() == 'N': print ("really i thought everybody likes piza.")
if c.upper() == 'N': sys.exit()
name = input("sooo what's ur name? ")
print("that a pretty good name i guess ")`

Upvotes: 2

Views: 47

Answers (1)

N. Ivanov
N. Ivanov

Reputation: 1823

You had made a mistake by just putting the or statement without specifying a condition. Instead of just oring a value, you have to specify another condition. Here I have provided an example how it should be.

import sys
c = input('do you like piza? Y or N:')
if c.upper() != 'N' or c.upper() != "Y": 
    print ("that wasn't an option")
if c.upper() != 'N' or c.upper() != "Y": 
    sys.exit()
if c.upper() == 'Y':
    print ('Me To') 
if c.upper() == 'N': 
    print ("really i thought everybody likes piza.")
if c.upper() == 'N': 
    sys.exit()
name = input("sooo what's ur name? ")
print("that a pretty good name i guess ")

You could also slightly refactor the code, by combining the last 2 ifs like so:

if c.upper() == 'N':
    print("really i thought everybody likes piza.")
    sys.exit()

and also the 1st two ifs like so:

if c.upper() != 'N' or c.upper() != "Y": 
    print ("that wasn't an option")
    sys.ext()

EDIT

The or should be replaced with an and, otherwise that check makes no sense.

Credit to khelwood for pointing it out.

Hope this helps!

Upvotes: 2

Related Questions