-2

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)
JordanDevelop
  • 143
  • 1
  • 6
  • 21

6 Answers6

1

because y is string

y = int(raw_input("Input second integer: "))
  • They say in their question that they are using python 3.3 – Xymostech Jan 30 '13 at 18:44
  • I have formatted your answer -- you can do that with the `{}`-button above the textarea – tzelleke Jan 30 '13 at 18:46
  • What seems to happen is, possibly, the tutorial I was following was not using Python 3.x. Is there anything I wrote that should be changed for 3.x language standards? – JordanDevelop Jan 30 '13 at 18:47
  • that's right if you were using python 2.x you could use input directly instead of int(raw_input('...')) .. but input is not available in python 3.x ,, input in 2.x job is evaluate the expression ,, you could read more about it – Complex Code Jan 30 '13 at 18:50
  • @ComplexCode your answer is wrong; he's asking about Python 3.x, not 2.x. Additionally, input should never be used (unless you absolutely know what you're doing) in python 2.x. Furthermore, raw_input was changed to input in 3.x, which means that input() is very much available. – Cassandra S. Jan 30 '13 at 20:09
0

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()

Xymostech
  • 9,710
  • 3
  • 34
  • 44
0

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.

Dinesh
  • 51
  • 4
0

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.

Cassandra S.
  • 750
  • 5
  • 20
0

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.

  • Here the calculator [link](https://ufile.io/w4ts1).It will expire after 30 days.. –  Jun 19 '17 at 20:25
  • Check new link to [calculator](https://www.protectedtext.com/mycalculator).type password: **password1**. copy the code paste it on your idle.And Enjoy. –  Jun 26 '17 at 23:44
-1

I figured it out. I was supposed to use:

int(float(input("Enter your meal price:")))

Thank you, everyone!

JordanDevelop
  • 143
  • 1
  • 6
  • 21