Python: I'm Making A Simple Calculator For Class. What's Wrong With This Code?
Solution 1:
because y is string
y = int(raw_input("Input second integer: "))
Solution 2:
I see a couple problems with your code.
Your use of print
on the first line will give you troubles, because print
is a function in Python 3. You should call it like print("hello")
.
On line 8, you have an extra colon:
calc():
Get rid of that.
Last, you don't need the semicolons when you call calc()
Solution 3:
This has a lot of changes to do. First, the first line of code should be:
print ("Hello")
Then, the raw_input() should become input().
Next, there should not be a colon or semicolon after calc() except on the second line when you are defining a function.
The code should look something like this. Try it.
print ("Hello")
defcalc():
x = int(input("Input first integer: "))
y = int(input("Input second integer: "))
type = str.lower(input("(A)dd, (S)ubstract, (M)ultiply, (D)ivide \n"))
iftype != "a"andtype != "s"andtype != "m"andtype != "d":
print ("Sorry, the command you entered is not valid.")
calc()
else:
iftype =="a":
print ("The result is '" + str(x+y) + "'")
eliftype == "s":
print ("The result is '" + str(x-y) + "'")
eliftype =="m":
print ("The result is '" + str(x*y) + "'")
eliftype == "d":
print ("The result is '" + str(float(x)/float(y)) + "'")
ifint(input("Enter 1 if you would like to perform another calculation? \n")) == 1:
calc()
else:
exit()
calc()
Hope this helps.
Solution 4:
Instead of converting to int (thus flooring whatever your value is -- 2.99 becomes 2, for instance) converting to float should do the trick nicely; the tip calculation should work even if you're doing 2.0*0.15 instead of 2*0.15.
This will obviously only work when you know what input you can expect. It will fail pretty badly if someone enters anything that isn't valid.
Solution 5:
I figured it out. I was supposed to use:
int(float(input("Enter your meal price:")))
Thank you, everyone!
Post a Comment for "Python: I'm Making A Simple Calculator For Class. What's Wrong With This Code?"