Python 用户输入产生“用户输入”;can';t将序列乘以类型为'的非整数;浮动'&引用;

Python 用户输入产生“用户输入”;can';t将序列乘以类型为'的非整数;浮动'&引用;,python,python-3.x,Python,Python 3.x,我是Python编程的初学者,目前正在使用CodeAcademy帮助我学习。所以我决定冒险去做一个自己的程序,一直被错误信息所困扰:不能用“float”类型的非int乘以sequence 该程序非常简单,是一个小费计算器,它要求用户输入信息,让程序确定小费金额和账单总额。直到数学的关键点,它还可以。我知道这并不“漂亮”,但我真的想知道如何使用它。任何帮助都将不胜感激 以下是我到目前为止的情况: print ("Restuarant Bill Calculator") print ("Instru

我是Python编程的初学者,目前正在使用CodeAcademy帮助我学习。所以我决定冒险去做一个自己的程序,一直被错误信息所困扰:不能用“float”类型的非int乘以sequence

该程序非常简单,是一个小费计算器,它要求用户输入信息,让程序确定小费金额和账单总额。直到数学的关键点,它还可以。我知道这并不“漂亮”,但我真的想知道如何使用它。任何帮助都将不胜感激

以下是我到目前为止的情况:

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)

您忘记将str转换为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)

您的问题是使用的是
input()
raw\u input()
混合使用。这是初学者经常犯的错误
input()
将代码作为Python表达式进行求值,并返回结果
raw_input()
但是,只需获取用户的输入并将其作为字符串返回即可

所以当你这样做的时候:

tip * cost 
你真正做的是:

2.5 * '20'
当然,这是毫无意义的,Python将引发一个错误:

>>>  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'
>>> 

请添加完整的错误消息,并填写错误行。一般来说,请阅读和阅读。(您可能正在尝试将字符串与浮点相乘)
#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)