Grady D
Grady D

Reputation: 2039

Python chars compare in if/else

The goal is to get a y or n and do different things based on the if/elseif/else statement. The problem is that it does not see y and n as proper values. Anyone know what I am doing wrong?

print 'Are you happy with the final crop?'
happyTest = raw_input('Enter y or n: ')
if happyTest == 'y':
    happy = False
elif happy == 'n':
    padding == int(raw_input('Enter crop padding:'))
else:
    print 'Not a valid input'

Upvotes: 0

Views: 58

Answers (2)

gabe
gabe

Reputation: 2511

In addition to icktoofay, who points out some problems in the code that I concur with, you should also probably use strip to make sure that the input doesn't have white spaces around it, which would throw off your comparison test.

Alternatively, try a comparison like

if "y" in happyTest:
     # then assume they meant "yes"
     # ...

Upvotes: 2

icktoofay
icktoofay

Reputation: 129109

You've got two problems I can see:

  1. elif happy == 'n': references an undefined variable, happy. You meant happyTest.

  2. padding == int(raw_input('Enter crop padding:')) tries to compare padding and int(...). You meant to assign. Change == to =.

Upvotes: 5

Related Questions