|
||||||
| Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I get an error for this code:
(there are indents just not showing on this forum) #small program to introduce members of the family print "Please enter the first name of a family member.", " ", "the choices are john, jane, jim, and scruffle." text = input() if text = "john" print "this is john" elif text = jane print "this is jane" elif text = jim print "this is jim" elif text = scruffle print "RUFF!!" |
| Sponsored Links |
|
|
|
|||||
|
First of all; the indents aren't showing, because you're not using the code-tags. Use them in your further posts.
There is four major problems in the code you posted. 1. input() is used for integers, and such, while raw_input() is used for text-input. Just think of it like: input() for numbers and raw_input() for strings. 2. Instead of using the compare-operator, ==, you're using the ***ignment operator, =. There's a big difference between these operators. 3. When your tries to compare in most of your if-statements, you're forgetting the double-quotes. These are necessary, or the interpreter will see them as variables - and you haven't declared those variable - which result in: error(s) 4. Languages like C++ are using curly brackets for statements, and such, while Python is using colons and indents. So, you need a ':' right after all the places where you're using a statement, e.g. if-statements. Look at this [corrected] code: Code:
print "Please enter the first name of a family member."
print "The choices are John, Jane, Jim, and scruffle."
text = raw_input()
if text == "John":
print "Hello, John"
elif text == "Jane":
print "Hello, Jane"
elif text == "Jim":
print "Hello, Jim"
elif text == "Scruffle":
print "RUFF!!"
else:
print "I don't know you, sorry!"
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||
|
Quote:
![]() Also with my revised code: Code:
#small program to introduce members of the family
print "Please enter the first name of a family member.", " ", "the choices are john, jane, jim, and scruffle."
text = raw_input()
if text == "john"
print "this is john":
elif text == jane
print "this is jane":
elif text == jim
print "this is jim":
elif text == scruffle
print "RUFF!!":
else:
print "Please retype!"
![]() Last edited by hds272; 10-15-2007 at 09:00 PM. |
|
|||||
|
Before the explanation of #3, I'll help you with the "invalid syntax" error. You need to add a colon (
in the end, right after "john" It will fix it. Look at my #4 in my last post.To the #3. Many programming languages (C, C++, C#, Java, Visual Basic, many LISP-dialects, etc.) and also Python, of course, uses special techniques to identify variables, strings, characters. It's different from language to language, but usually you're using single-quotes for characters, double-quotes for strings and nothing for variables. Try to pick up some tutorial, it will clear it up for you - much better than I tried - and you'll understand.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Xav | ........ | 1323.18 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 837.78 |
| Brandon W | ........ | 749.07 |
| chili5 | ........ | 310.39 |
| Steve.L | ........ | 241.84 |
| dcs | ........ | 216.02 |
Goal: 100,000 Posts
Complete: 82%