Reputation: 33
very basic question I'm afraid! I'm a Python newbie, just getting started. I've tried a few ways of getting the following to work, and now I'm wondering whether there was a module I need to import to get raw_input to work? I'm on Python 2.7.10. I want to print "Jane is awesome" if the user input is Jane, and for all other user inputs, print name + "is not awesome". But it always prints name + "is not awesome", even when I enter Jane.
1)
while True:
name = raw_input("What is your name?")
if name == "Jane":
print name + " is awesome"
break
else:
print name + " is not awesome"
2)
endprogram = 0
while endprogram != 1:
name = raw_input ("What is your name?")
while name != "Jane":
print name + " is not awesome"
name = raw_input ("What is your name?")
print "Jane is awesome!"
endprogram = 1
Upvotes: 3
Views: 116
Reputation: 1122092
Both pieces of code work just fine. Enter Jane
and it'll break out:
>>> while True:
... name = raw_input("What is your name?")
... if name == "Jane":
... print name + " is awesome"
... break
... else:
... print name + " is not awesome"
...
What is your name?Martijn
Martijn is not awesome
What is your name?Jane
Jane is awesome
Add in a repr()
function call to help debug any issues you may have, such as additional whitespace or missing capitalisation or special characters other than ASCII:
>>> while True:
... name = raw_input("What is your name?")
... print 'You entered:', repr(name)
... if name == "Jane":
... print name + " is awesome"
... break
... else:
... print name + " is not awesome"
...
What is your name?Martijn
You entered: 'Martijn'
Martijn is not awesome
What is your name? Jane
You entered: ' Jane'
Jane is not awesome
What is your name?jané
You entered: 'jan\xc3\xa9'
jané is not awesome
What is your name?Jane
You entered: 'Jane'
Jane is awesome
Note the initial space in my first 'attempt' to enter Jane
(the quotes around the value show there is a space there). When entering an é
, my terminal sends UTF-8 bytes to Python, showing up as two escaped values, hex C3 and A9.
Upvotes: 3