User- Input Calculator


#1

I tried to expand on this course by having a user give their input step by step, but I ran into trouble:

`meal = input ('enter meal cost: $')
tax = input ('enter tax amount: $')
tip = input ('what percentage should the tip be (please type in decimals) : $')

meal = meal + meal * tax
total = meal + meal * tip

print ("Here is Your Total:")
print ("%.2f" % total)

The error code says
can't multiply sequence by non-int of type 'str'`

Is there a way to convert strings (from the input) into integers? I would appreciate your help.


#2

Edit: Sorry, that's JS.

You could use parseInt(placeholder, base) replacing placeholder with some string to be converted to a number (or a variable name), and base replaced with the base you want it in (10 would return the number in base 10, which is what is used in everyday life).

OR

You could use Number(placeholder) replacing placeholder with some string to be converted to a number (or a variable name).

See here for info on the differences between the two.


#3

In Python

int(placeholder) to convert it to an integer.

float(placeholder) to convert to a float.


#4

Thanks again for your help. I found that the new code worked with whole numbers, but not decimals for some reason (e.g. 0.15)- why is this?

`meal = input("enter meal cost: $")
tax = input("enter tax amount: $")
tip = input("what percentage should the tip be (please type in decimals): ")

meal = int(meal) + int(meal) * int(tax)
total = meal + meal * int(tip)

print ("Here is Your Total:")
print ("%.2f" % total)
`


#5

@jswald17 you need to use float(placeholder) for a floating point (which is a decimal).


#6

@dantemp

Thanks so much! The code works great! :relaxed: