473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need help figuring out how to fix this code.

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/

--
----------------------------------------------------------------
Posted via UsenetRevolutio n.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
Jul 19 '05 #1
8 1638
Hi,
1) check the spacing before if name == "Nathan":
2) put a ':' after else

Regards,

Philippe
Nathan Pinno wrote:
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/


Jul 19 '05 #2
On 6/28/05, Nathan Pinno <fa********@hot mail.com> wrote:
Hi all,
[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/

Jul 19 '05 #3
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 -

--
You can imagine the opposite
-- Maurizio Nannucci

Jul 19 '05 #4
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


On 6/28/05, Nathan Pinno <fa********@hot mail.com> wrote:
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/



--


----------------------------------------------------------------
Posted via UsenetRevolutio n.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #5
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?

Jul 19 '05 #6
On Tue, 28 Jun 2005 14:39:47 -0400, Nathan Pinno wrote
(in article <42************ **********@news .usenetrevoluti on.com>):
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
Jul 19 '05 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan Pinno wrote:
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

iD8DBQFCweCuctN FyQJObrsRAuXsAJ 0auOEcnZDZB/A8hLHNS7D5C1Rl2 ACfQNp1
7PAZLqG7H/6Fv6hC2m9CO50=
=cesa
-----END PGP SIGNATURE-----
Jul 19 '05 #8
Hi Nathan,

Please see my comments listed below.

Nathan Pinno wrote:
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
---
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1772
by: Sam Watson | last post by:
Hi, I could use a little help with a project i'm going to build. I know I want to use python and wxWindows but thats about all I know. The client will be linux or windows. The server will be linux. Basically, its a client/server app with a rich GUI client. The big catch is I want to be able to run the client/server remotely across the...
1
1789
by: Michael Brown | last post by:
I've got some code that looks somewhat like this: typedef struct TAG_STREAMDATA { // Lots of stuff in here ... double param1, param2; } STREAMDATA;
12
3858
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm...
2
1520
by: kristopher.erickson | last post by:
Hereis some basic code that works fine: For Each itm In fld.Items I = I + 1 j = 1 Set rng = wks.Cells(I, j) If itm.Start <"" Then rng.Value = itm.Start j = j + 1
2
1800
by: Anders B | last post by:
I want to make a program that reads the content of a LUA array save file.. More precicely a save file from a World of Warcraft plugin called CharacterProfiler, which dumps alot of information about your characters into that save file. Anyhow, I want to extract a couple of lines of it and save it into a database and I need help on figuring...
9
2623
by: wparrott | last post by:
I have a table called tblPayScales. It has 5 fields: numRecID - record ID txtCLASSCODE - a 1 or 2 letter text value (A, B, C, BD, L, LT, etc.) numPAYSTEP - a number value 0-49 numSALARY - a number value such as 36,246.00 numNEWSALARY - a number value derived from a user-defined percentage The table contains records for each...
6
1660
by: squishy | last post by:
I tried to hire programmers to do some stuff, but cannot find reliable, intelligent C++ Gurus (at Guru or RentACoder at least). So I am stuck learning C++ and doing the jobs myself. I would like to streamline my studying to avoid studying anything that would only serve to slow my progress while not really adding to the advancement of my...
4
2532
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type vector's Iterator in C#? The problem is in the next line and the rest of the code is just additional information on what I'm doing. Thanks....
1
7082
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.