1

I am a beginner here as well as programming in Python, and I am currently using code academy to help me learn. So I decided to venture off and make a program on my own and keep getting stuck with error message: can't multiply sequence by non-int of type 'float'

The program is very simple, a tip calculator where it asks the user to input information to have the program determine the amount of tip and total amount of bill. And it does ok up until the point of the math. I know it's not "pretty" but it's just me really trying to figure out how to work it. Any help would be greatly appreciated!

Here is what I have so far:

print ("Restuarant Bill Calculator")
print ("Instructions: Please use only dollar amount with decimal.")

# ask the user to input the total of the bill
original = raw_input ('What was the total of your bill?:')
cost = original
print (cost)

# ask the user to put in how much tip they want to give
tip = input('How much percent of tip in decimal:')
tipamt = tip * cost      
print "%.2f" % tipamt

# doing the math
totalamt = cost + tipamt
print (totalamt)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

2 Answers2

0

You forget to transform the str into float:

original = raw_input('What was the total of your bill?:')
cost = float(original)
print (cost)

#ask the user to put in how much tip they want to give
tip = input('How much percent of tip in decimal:')
tipamt = tip * cost      
print("%.2f" % tipamt)

#doing the math
totalamt = cost + tipamt
print (totalamt)
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

Your problem is that you are using input() mixed with raw_input(). This is a common mistake for beginners to make. input() evalutes your code as if it were a Python expression and returns the result. raw_input() however, simply get's input and returns it as a string.

So when you do:

tip * cost 

What you're really doing is something like:

2.5 * '20'

Which of course, makes no sense and Python will raise an error:

>>>  2.5 * '20'
Traceback (most recent call last):
  File "<pyshell#108>", line 1, in <module>
    '20' * 2.5
TypeError: can't multiply sequence by non-int of type 'float'
>>> 

You need to first use raw_input() to get the cost, then cast that to an integer. And then use taw_input() to get the tip as a string, and cast the input to a float:

#ask the user to input the total of the bill

# cast input to an integer first!
original = int(raw_input('What was the total of your bill?:'))
cost = original
print (cost)

#ask the user to put in how much tip they want to give

# cast the input to a float first!
tip = float(raw_input('How much percent of tip in decimal:'))
tipamt = tip * cost      
print "%.2f" % tipamt

#doing the math
totalamt = cost + tipamt
print (totalamt)
Christian Dean
  • 22,138
  • 7
  • 54
  • 87