-1

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:

  • For a person named Amar, respond by saying Hi Amar :)
  • For a person named Brandy, respond by saying 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.

HappyHands31
  • 4,001
  • 17
  • 59
  • 109
  • Even though a syntax error was one of the reasons why my code didn't work, other reasons included mistaking strings for variables, understanding how quotation marks should go around strings in python, and how to use nested If statements in python. – HappyHands31 Oct 10 '16 at 22:24

2 Answers2

2
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':
Uriel
  • 15,579
  • 6
  • 25
  • 46
  • I fixed line 4 and line 6, but now I'm getting the message "NameError: name 'raw_input' is not defined" ...I know it's not defined because a dialogue box isn't showing up for me to enter the name. Any idea why? – HappyHands31 Oct 10 '16 at 19:37
  • @HappyHands31 maybe you are using python 3? python 3 has simply `input` instead of `raw_input` – Uriel Oct 10 '16 at 19:47
  • Switched to version 2 on the website (http://www.pythontutor.com/live.html#mode=edit), still not printing the correct output. You can see the exact code the I'm using in the edited section of my question. – HappyHands31 Oct 10 '16 at 19:50
  • You'll notice that it works when you enter Amar or Brandy, but not when entering another name (i.e. Steven). If you enter Steven, you just get "Hello + " – – HappyHands31 Oct 10 '16 at 19:55
  • 1
    Well, because you didn't tell it to print `Name`, but `hello +`. Use `print('Hello', Name) ` – Uriel Oct 10 '16 at 20:49
0

if "" = Amar:

First, when checking for equality, use == instead of =.

Second, why are you checking a blank string ""? Use Name instead.

Third, the compared value must itself be in quotes: "Amar" instead of Amar.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I fixed line 4 and line 6, but now I'm getting the error message "NameError: name 'raw_input' is not defined" ...I know it's not defined because a dialogue box isn't showing up for me to enter the name. Any idea why not? – – HappyHands31 Oct 10 '16 at 19:38
  • What version of Python are you using? Python 2 uses `raw_input`, but Python 3 uses `input`. And in any case I think it would use the console window, not a popup dialog box. – John Gordon Oct 10 '16 at 19:42
  • Alright, I switched the version to 2.7. Yeah it's using a console window. But I am still not printing the correct output - you can see the exact code that I'm using in the edited section of my question. – HappyHands31 Oct 10 '16 at 19:48
  • Specifically, it works when I enter Amar or Brandy, but not when entering another name (i.e. Steven). If I enter Steven, I just get "Hello + " – HappyHands31 Oct 10 '16 at 19:54