skip to Main Content

I’m new to Python and while trying to write a scrip that will keep asking questions about the user until the scrip gets FALSE,

I decided to check the scrip,of course it gave me an syntax error that told me the mistake was on the fifth lane, `a.

Now on that lane I tried to change the old value of a to a new value.
sadly, I can’t understand the mistake that I made, can some one please check it and explain me what went wrong ?

    #!/usr/bin/python

print "Hello, I'm wilfred and I'm an Artificial Intelligencen"

a=str(raw_input("Do you want to be my friend? n"))

if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?n"))               
    if a==str :
        print "Nice name man!"
    elif a==int :
        print "bye!"
elif a=="no":
    print "Well, nice to meet you anway, good bye now n"

3

Answers


  1. Your line

    a=str(raw_input("Yey ! my first friend,what is your name?n")  
    
    • Indent this line so it is inside the ‘if’ statement
    • Add a ‘)’ at the end of this line
    Login or Signup to reply.
  2. A general structure for this kind of repeating loop is

    while True:
        a=str(raw_input(...)) 
        if a=="whatever": break
        # other responses to a
    
    Login or Signup to reply.
  3. You just need to indent the line. Your code should work fine. Keep learning python. It’s awesome!!!!

    #!/usr/bin/python
    
    print "Hello, I'm wilfred and I'm an Artificial Intelligencen"
    
    a=str(raw_input("Do you want to be my friend? n"))
    
    if a=="yes":
        a=str(raw_input("Yey ! my first friend,what is your name?n"))               
        if a==str :
            print "Nice name man!"
        elif a==int :
            print "bye!"
    elif a=="no":
        print "Well, nice to meet you anway, good bye now n"
    

    To further help with the test cases, I changed your string and int tests for you. “==” test is for value btw.

    #!/usr/bin/python
    
    print "Hello, I'm wilfred and I'm an Artificial Intelligencen"
    
    a=str(raw_input("Do you want to be my friend? n"))
    
    if a=="yes":
        a=str(raw_input("Yey ! my first friend,what is your name?n"))
        if a.isalpha() :
            print "Nice name man!"
        elif a.isdigit() :
            print "bye!"
    elif a=="no":
        print "Well, nice to meet you anway, good bye now n"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search