Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Need Help With My Python Programs, just extremely basic stuff
arcanistherogue
post Feb 1 2005, 02:17 AM
Post #1


Advanced Member
*******

Group: Members
Posts: 109
Joined: 25-January 05
Member No.: 3,417



ok, i am learning python, and i realy dont know much. my guide im using said to make a program where it asks your name, and if the name is yours your make it so there is a compliment, if it is another name you make it that its an insult, and anything else makes it say Nice name.

here is what i wrote

name = raw_input("What is your name?")
if name == John:
print "Your name is freaking sweet, you must be a god or something."
elif name == Bob:
print "You have a freakin' weird name, dude."
else:
print "Nice name,",name


i donot understand what i am doing wrong, i get this error when i run it:

What is your name? John

Traceback (most recent call last):
File "C:/Python23/name.py", line 4, in -toplevel-
if name == John :
NameError: name 'John' is not defined

can someone help me please?

thanks for any response.
Go to the top of the page
 
+Quote Post
arcanistherogue
post Feb 2 2005, 10:19 PM
Post #2


Advanced Member
*******

Group: Members
Posts: 109
Joined: 25-January 05
Member No.: 3,417



bump... please, i need some help with this....
Go to the top of the page
 
+Quote Post
bjrn
post Feb 2 2005, 11:33 PM
Post #3


Super Member
*********

Group: Members
Posts: 378
Joined: 8-January 05
Member No.: 3,174



Uhm, perhaps you need to add quotes around "John" and "Bob" in the if statement?
Go to the top of the page
 
+Quote Post
arcanistherogue
post Feb 3 2005, 01:04 AM
Post #4


Advanced Member
*******

Group: Members
Posts: 109
Joined: 25-January 05
Member No.: 3,417



by God.. it worked!

you. cookie. now.
Go to the top of the page
 
+Quote Post
bjrn
post Feb 3 2005, 10:50 AM
Post #5


Super Member
*********

Group: Members
Posts: 378
Joined: 8-January 05
Member No.: 3,174



CHOMP CHOMP CHOMP.

Very understandable error. Just like forgetting ';' in languages that need it (PHP, Java and so on). I'm just glad you hadn't got to somewhere where you had made hundreds of lines of code (and posted here) tongue.gif

I'm glad I could help and good luck with the further learning smile.gif
Go to the top of the page
 
+Quote Post
arcanistherogue
post Feb 7 2005, 09:30 PM
Post #6


Advanced Member
*******

Group: Members
Posts: 109
Joined: 25-January 05
Member No.: 3,417



thanks alot.

right now im working on a math helper program, that has a list of many formulas. It will start out as a list of basic things, like area, etc, then when you select one it goes more in depth, like area of a quadrilateral, then maybe like area of a square or whatnot. what i want to have it do is to have a loop back to the main page after each formula, and i put if selection == 0 it displays the main screen, and all of the formulas have a number near them so you input the corressponding number to select the certain formula. what i want to know is, this is a basic idea of what i have, i want to know if it will work:

selection = 0
if selection ==0:
print "choose a formula"
print "1. area"
selection = raw_input
if selection == 1:
print "a. area of a square"
selection = raw_input
if selection == a
a = raw_input("Length of a side?")
b = a*a
print b
selection = 0

will that work? like will it loop back at the end of the formula?

and also, can i have multiple If's, or do i just put in multiple elif's?
Go to the top of the page
 
+Quote Post
bjrn
post Feb 8 2005, 09:22 AM
Post #7


Super Member
*********

Group: Members
Posts: 378
Joined: 8-January 05
Member No.: 3,174



QUOTE(arcanistherogue @ Feb 7 2005, 10:30 PM)
and also, can i have multiple If's, or do i just put in multiple elif's?

I have to admit that I know very little about Python, so I don't really know how it would work if you wanted it to jump back to the beginning sometimes.

What I can tell you is that you shouldn't use elif's for the bit that you've written so far, once you have some more options on each level you can do that. But those if's you have at the moment should stay if's, because they are conditions that shouldn't be checked before the if before them has been checked.
Go to the top of the page
 
+Quote Post
s243a
post Mar 25 2005, 12:09 AM
Post #8


Super Member
*********

Group: Members
Posts: 408
Joined: 7-December 04
From: Nova Scoita, Canada
Member No.: 2,604



I agree with what the above poster said. I find case neater but some books suggest avoiding it. I don’t know if python has a case. If not use else if although you could just as well use if. It is just less efficient and perhaps less readable. Also if you mathhelpers become long it is better to make them functions. On a side note I thought python was a functional programming language. From the code you showed me it looks like an imperative language like c.
Go to the top of the page
 
+Quote Post
Trap FeedBacker
post Apr 23 2008, 04:05 AM
Post #9


Guest Feedbacks
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



python
Need Help With My Python Programs

Hi,

I am very new to python.I need to write a program for a cluster.
Imagine a situation where there are three machines A,B,C clustered in a domain.If one machine goes down the other two should get notified by a message/email.Is it possible to write a script for that??Please help

-question by Kalpana
Go to the top of the page
 
+Quote Post
Trap FeedBacker