-
Just starting with python, encountering 1 problem...
I have just started to learn python, and I am loving it, I have tried to learn other languages before, but this is really workin, but I am having one problem,
Any time I have an "If" statement, Such as:
if shape = 1:
or
if temperature > 50:
Anyone know the problem?
-
Here is a complete Code I am using as I am just starting out,
Code:
# Area Calculation Program
print "Welcome to the Area Calculation program"
print "---------------------------------------"
print
# Print out the menu:
print "Please select a shape:"
print "1 Rectangle"
print "2 Circle"
# Get the user's choice:
shape = input("> ")
#calculate the area:
if shape = 1:
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area = height*width
print "The area is", area
else:
radius = input("Please enter the radius: ")
area = 3.14(radius**2)
print "The area is", area
-
And, err, what's wrong with it? Error messages? Does it just not run right?
-
Hah, I am a idiot, didn't post the error message..
Code:
File "C:/Documents and Settings/Russell Johnston/My Documents/Python stuff/askcalc", line 16
if shape = 1:
^
SyntaxError: invalid syntax
-
==
'Scuse me while I kiss the sky
-
Thank you so much!
-
New problem though --
Code:
Please select a shape:
1 Rectangle
2 Circle
> 2
Please enter the radius: 2
Traceback (most recent call last):
File "C:\Documents and Settings\Russell Johnston\My Documents\Python stuff\askcalc", line 23, in ?
area = 3.14(radius^2)
TypeError: 'float' object is not callable
I am assuming something is going wrong with the process where it cannot determine the answer, hence the float, I am guessing though.
-
Meh, I fixed it, Just did radius*radius*3.14 instead.
-
New Problem...
Code:
temperature = input("What is the temperature of the spam?")
if temperature > 50:
print "The salad is properly cooked."
else:
print "Cook the salad some more."
The problem is with the "if", Which I cannot find out the problem to.
-
Originally posted by Russ356
New problem though --
Code:
Please select a shape:
1 Rectangle
2 Circle
> 2
Please enter the radius: 2
Traceback (most recent call last):
File "C:\Documents and Settings\Russell Johnston\My Documents\Python stuff\askcalc", line 23, in ?
area = 3.14(radius^2)
TypeError: 'float' object is not callable
I am assuming something is going wrong with the process where it cannot determine the answer, hence the float, I am guessing though.
Text(stuff) is the syntax for a function call (where Text is the name of the function). That's what it was parsing it as. Also the exponentiation operator in python is **, not ^. You want:
Code:
area = 3.14 * (radius**2)
-
Originally posted by Russ356
New Problem...
Code:
temperature = input("What is the temperature of the spam?")
if temperature > 50:
print "The salad is properly cooked."
else:
print "Cook the salad some more."
The problem is with the "if", Which I cannot find out the problem to.
Error message please.
-
Actually, I see an error
input (use raw_input, btw) returns a string, which you can't compare to an integer. Either cast the input to an integer or ... yeah, just do that.
-
I am assuming the raw helped but the error message says
File "C:\Documents and Settings\Russell Johnston\My Documents\Python stuff\Spamsaladtemp.py", line 3
if temperature > 50:
^
SyntaxError: invalid syntax
-
I got a new Error too!
Code and message:
Code:
if f == 1:
# Do something...
elif f == 2:
# Do something else...
elif f == 3:
# Do something completely different...
else:
# If all else fails...
File "C:/Documents and Settings/Russell Johnston/My Documents/Python stuff/elseif.pyw", line 3
elif foo == 2:
^
SyntaxError: invalid syntax
It seems it doesn't like when I use "if" any time.
-
Originally posted by Russ356
I got a new Error too!
Code and message:
Code:
if f == 1:
# Do something...
elif f == 2:
# Do something else...
elif f == 3:
# Do something completely different...
else:
# If all else fails...
File "C:/Documents and Settings/Russell Johnston/My Documents/Python stuff/elseif.pyw", line 3
elif foo == 2:
^
SyntaxError: invalid syntax
It seems it doesn't like when I use "if" any time.
Empty if/elif/else clauses are syntax errors. You can't have an if statement with no thing in it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|