问题
My teacher requests me to make a calculator that can calculate a 15% tip on a submitted price. I followed an online tutorial for this python application. I made a simple addition, subtraction, multiplication, and division calculator for practice before I make what my teacher requires. In the YouTube video I followed the python application ran on his computer. I get a a "Invalid Syntax" error in the Python Shell. I'm using Python 3.3.0. Thank you.
EDIT: I followed a different tutorial figured out how to finish my project. The only problem I'm having is using both regular integers (1, 2, 3, 4, etc.) and float integers (4.36, 5.45, etc.).
print("Welcome to the calculator!")
mealPrice = int(input("Enter your meal price:"))
asw = mealPrice * 0.15
print("The tip you owe is: ", asw)
回答1:
because y is string
y = int(raw_input("Input second integer: "))
回答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()
回答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")
def calc():
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"))
if type != "a" and type != "s" and type != "m" and type != "d":
print ("Sorry, the command you entered is not valid.")
calc()
else:
if type =="a":
print ("The result is '" + str(x+y) + "'")
elif type == "s":
print ("The result is '" + str(x-y) + "'")
elif type =="m":
print ("The result is '" + str(x*y) + "'")
elif type == "d":
print ("The result is '" + str(float(x)/float(y)) + "'")
if int(input("Enter 1 if you would like to perform another calculation? \n")) == 1:
calc()
else:
exit()
calc()
Hope this helps.
回答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.
回答5:
If you want to use integer:
mealprice = int(input("Enter your meal price:"))
If you want to use float:
mealprice = float(input("Enter your meal price:"))
Float is recommended because You can also use a single number like 1,2,3 etc and also float numbers.
I also created my own calculator that has 6 functions:
1 = Add
2 = Subtract
3 = Multiply
4 = Divide
5 = Round Off
6 = Get Remainder
It is so simple.
I have Not Used Round Off yet.
If You want That code Ask me I will give you.
If you want more Info, I am ready to Provide To you.
回答6:
I figured it out. I was supposed to use:
int(float(input("Enter your meal price:")))
Thank you, everyone!
来源:https://stackoverflow.com/questions/14611344/python-im-making-a-simple-calculator-for-class-whats-wrong-with-this-code