raw_input:我不知道为什么我的python代码不起作用

时间:2015-02-05 01:52:28

标签: python

我是python的新手,我试图制作一个代码。我希望你输入你的餐费,然后是你所在地区的税,然后是你的小费。

meal=raw_input("What was the cost of your meal?")
tax=raw_input("What is the tax in your area?")
tip=raw_input("What will be your tip percentage?")
print meal*tax+tip

2 个答案:

答案 0 :(得分:2)

raw_input返回一个字符串。您必须将输入值转换为数字:

meal = float(raw_input("What was the cost of your meal?"))
tax = float(raw_input("What is the tax in your area?"))
tip = float(raw_input("What will be your tip percentage?"))
print(meal * tax + tip)

答案 1 :(得分:0)

最好先检查基本的Python教程。因为它非常基础,raw_input返回字符串。在其他情况下,这是一封信,记住在Python中"4"4不同。第一个是字符串,第二个是数字。

由于您没有将变量转换为整数或浮点数,因此无法进行数学运算。你能说出a*ba+b是什么吗?不,所以Python不能。正如我所说"4"4不同,所以你不能像"4"+"4". It's equal to a + b . raw_input返回字符串这样的操作,你必须转换那些变量整数或先浮动,然后你就可以进行数学运算了。

meal = float(raw_input("What was the cost of your meal?"))
tax = float(raw_input("What is the tax in your area?"))
tip = float(raw_input("What will be your tip percentage?"))
print (meal * tax + tip)
#do your math operations