I need help figuring out how to fix this code.

N

Nathan Pinno

Hi all,

I need help figuring out how to fix my code. I'm using Python 2.2.3, and
it keeps telling me invalid syntax in the if name == "Nathan" line. Here is
the code if you need it.

#This program asks for a password, then asks for the user's name after the
correct password has been supplied. The computers response will vary,
# depending on the name inputted.
print "Program Author: Nathan Pinno"
print "ID# 2413448"
print
print "Program 3 - Loops and IF Conditions"
print
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
print "Welcome to the second half of the program!"
name = raw_input("What is your name, please? ")
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:
print "May I have your autograph please!"
else
print name,", that's a nice name!"

What's wrong with the code? How do I fix it, so that it works?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/
 
P

Philippe C. Martin

Hi,
1) check the spacing before if name == "Nathan":
2) put a ':' after else

Regards,

Philippe
 
J

Jaime Wyant

[snip!]

It looks like your indentation is off for the if statement. It should
be aligned with the "name = raw_input" statement above it.

Also, elif name == ["Madonna", "Cher"]: will never evaluate to true.
Assume someone enters "guido" for their name, then you're doing this
comparison.

"guido" == ["Madonna", "Cher"]

A string will never equal a list. Now, a string could be in a list.

if name in ("Madonna", "Cher"):
print "Sorry, you can't come in."

hth,
jw
#This program asks for a password, then asks for the user's name after the
correct password has been supplied. The computers response will vary,
# depending on the name inputted.
print "Program Author: Nathan Pinno"
print "ID# 2413448"
print
print "Program 3 - Loops and IF Conditions"
print
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
print "Welcome to the second half of the program!"
name = raw_input("What is your name, please? ")
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:
print "May I have your autograph please!"
else
print name,", that's a nice name!"

What's wrong with the code? How do I fix it, so that it works?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/
 
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

Make sure that line with name=="Nathan" is not indented. It's hard to
tell from the code there.

Also, I'm thinking that this won't work:
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:

because the variable name is a string and not a list. You could try:
elif name in ["Madonna", "Cher"]:

Greg
 
H

harold fellermann

Hi,
I need help figuring out how to fix my code. I'm using Python 2.2.3,
and
it keeps telling me invalid syntax in the if name == "Nathan" line.

The problem is that you indent the if statement. the if/elif/else
statements
are part of the outer block, so they do not need indentation.
Here is the code if you need it.
#This program asks for a password, then asks for the user's name
after the
correct password has been supplied. The computers response will vary,
# depending on the name inputted.
print "Program Author: Nathan Pinno"
print "ID# 2413448"
print
print "Program 3 - Loops and IF Conditions"
print
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
print "Welcome to the second half of the program!"
name = raw_input("What is your name, please? ")
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:
print "May I have your autograph please!"
else
print name,", that's a nice name!"

name = raw_input("What is your name, plase? ")
if name == "Nathan" :
print "What a great name!"
elif name in ["Madonna","Cher"] : # in better than == here :)
print "May I have your autograph please!"
else : # don't forget the ":"
print name, ", thats a nice name!"


cheers,

- harold -
 
D

Devan L

password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
Wouldn't this print "Incorrect password" untill the end of time if you
didn't supply the correct password?
 
C

Chinook

Hi all,

I need help figuring out how to fix my code. I'm using Python 2.2.3, and
it keeps telling me invalid syntax in the if name == "Nathan" line. Here is
the code if you need it.

#This program asks for a password, then asks for the user's name after the
correct password has been supplied. The computers response will vary,
# depending on the name inputted.
print "Program Author: Nathan Pinno"
print "ID# 2413448"
print
print "Program 3 - Loops and IF Conditions"
print
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
print "Welcome to the second half of the program!"
name = raw_input("What is your name, please? ")
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:
print "May I have your autograph please!"
else
print name,", that's a nice name!"

What's wrong with the code? How do I fix it, so that it works?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/

At least one problem is further within the "if" >

Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)]
Type "help", "copyright", "credits" or "license" for more information.
name = 'yosa'
test = ['yosa', 'sosa']
name == test False
name in test True

Lee C
 
?

=?ISO-8859-1?Q?Elmo_M=E4ntynen?=

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan said:
Hi all,

I need help figuring out how to fix my code. I'm using Python 2.2.3, and
it keeps telling me invalid syntax in the if name == "Nathan" line. Here is
the code if you need it.

#This program asks for a password, then asks for the user's name after the
correct password has been supplied. The computers response will vary,
# depending on the name inputted.
print "Program Author: Nathan Pinno"
print "ID# 2413448"
print
print "Program 3 - Loops and IF Conditions"
print
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
print "Welcome to the second half of the program!"
name = raw_input("What is your name, please? ")
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:
print "May I have your autograph please!"
else
print name,", that's a nice name!"

What's wrong with the code? How do I fix it, so that it works?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/
I think about all the problems in your code are solved by suggestions
sent before me, but I have some advice for future situations:

Especially with more complex problems, larger code blocks using external
libs, etc, you should, if appropriate, include the Traceback, some
explanation on the given code(what it should do etc.) and maybe consider
documenting the code a little with comments and docstrings(try reading
some code you haven't seen before or in a long time, without any
commentation. What about debugging it without knowing what the code is
supposed to do?) Don't be intimidated by this, you just seemed to be new
with programming with python and thought of sharing this with you. You
have maybe read something like this before from a book or tutorial(there
are a couple of good free books for learning python, for example "byte
of python"), but good commenting and the like are usually appreciated
only when they're not available:)

Hope I didn't scare you. Happy tinkering with python!

Elmo Mäntynen
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCweCuctNFyQJObrsRAuXsAJ0auOEcnZDZB/A8hLHNS7D5C1Rl2ACfQNp1
7PAZLqG7H/6Fv6hC2m9CO50=
=cesa
-----END PGP SIGNATURE-----
 
B

Brian

Hi Nathan,

Please see my comments listed below.

Nathan said:
print "Program Author: Nathan Pinno"
print "ID# 2413448"
print
print "Program 3 - Loops and IF Conditions"
print
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"


At this point, you need to add the following statement again:
password = raw_input("Type in the password, please: ")

Otherwise, it just keeps stating "Incorrect password!" forever without
giving the user the ability to re-enter another password attemp / try.

print "Welcome to the second half of the program!"
name = raw_input("What is your name, please? ")
if name == "Nathan":
print "What a great name!"
elif name == ["Madonna", "Cher"]:
print "May I have your autograph please!"
else
print name,", that's a nice name!"


Remove the tab indentions in front of the "if", "elif", and "else"
statements. This will cause an error in your program. The only time
that you need to indent is the code following the if statements.

if name == "Steven":
print "Then I indent this code here."
else:
print "This is another coded statement here."

Thanks,
Nathan Pinno


You're welcome, Nathan!

Hope this helps (HTH),

Brian
---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top