In this program, I must ask the user What is your name?
, and then respond by printing out a personalized greeting.
In general, for a person named NAME
, respond by printing Hello NAME
.
Eg. For a person named Maria
, respond by printing Hello Maria
There is a special rule, however, for Amar and Brandy. These two names should receive unique greetings (and smiley faces), such that:
Hi Amar :)
Ahoy Brandy :D
The robot grader will only mark my solution correct if my print statements match EXACTLY what was specified above.
Here is what I tried in a Live Python Editor
Name = ""
# Prompt user for user-defined information
Name = raw_input('What is your Name? ')
if "" = Amar:
print ("Hi Amar :)")
if "" = Brandy:
print ("Ahoy Brandy :D")
else:
print ("Hello + """)
This code returned an error message on line four:
SyntaxError: invalid syntax (<string>, line 4)
How may I improve this code so that there are no errors and the program runs as expected?
EDIT I fixed the errors on lines 4 and 6, so now my code looks like this:
Name = ""
# Prompt user for user-defined information
Name = raw_input('What is your Name? ')
if Name == 'Amar':
print ("Hi Amar :)")
if Name == 'Brandy':
print ("Ahoy Brandy :D")
else:
print ("Hello + """)
But I'm still getting an error message:
"NameError: name 'raw_input' is not defined"
How do I define 'raw_input'? Don't I define it by entering a name into a dialogue box? (No dialogue box is showing up)
EDIT 2 Learned that raw_input is not compatible with version 3 of Python - switched Python editor to version 2. The code now works for names "Amar" and "Brandy" but not for any other name - we still get "Hello + " instead of "Hello Steven". Any idea why this is happening?
EDIT 3 The key to getting the program to run exactly how I wanted was to use a Nested Conditional - I needed to put an if statement inside of an if statement to make sure that both conditions were met.
name = input('What is your name? ')
if name != 'Amar':
if name != 'Brandy':
print("Hello " + name)
if name == 'Amar':
print("Hi Amar :)")
if name == 'Brandy':
print("Ahoy Brandy :D")
This says that if the name entered is not Amar or Brandy, print "Hello" + the name. If the name entered is Amar or Brandy, though, print according to their unique conditions.
if "" = Amar:
tries to assign a variable named Amar
(that doesn't exist) to an empty string (meaningless).
Watch out for the =
assignement operator and ==
equality operator.
You want to check if Name
equals to "Amar":
if Name == 'Amar':