Python 为什么我们不能在函数中使用原始输入?

Python 为什么我们不能在函数中使用原始输入?,python,Python,以下代码的错误有疑问:-(我想这样做,以便我们可以输入值,该值可以作为变量) 显示以下内容时出错: Traceback (most recent call last): File "python", line 31, in <module> File "python", line 23, in trip_cost File "python", line 17, in rental_car_cost TypeError: unsupported operand type(s

以下代码的错误有疑问:-(我想这样做,以便我们可以输入值,该值可以作为变量)

显示以下内容时出错:

Traceback (most recent call last):
  File "python", line 31, in <module>
  File "python", line 23, in trip_cost
  File "python", line 17, in rental_car_cost
TypeError: unsupported operand type(s) for -=: 'str' and 'int'
回溯(最近一次呼叫最后一次):
文件“python”,第31行,在
文件“python”,第23行,在trip_cost中
文件“python”,第17行,在租车成本中
TypeError:-=:“str”和“int”的操作数类型不受支持

您需要在此处使用
int
float

city = raw_input("Which city will you stay in ")
days = int(raw_input("How long is the stay "))
spending_money = float(raw_input("How much is your spening budget "))

您需要在此处使用
int
float

city = raw_input("Which city will you stay in ")
days = int(raw_input("How long is the stay "))
spending_money = float(raw_input("How much is your spening budget "))

我看不出问题中写的错误。但是您有几个问题,例如缩进和跨越多行的
return
语句。在这段代码中,您没有在函数中使用
raw\u input
raw\u input
返回
str
。但是在
trip\u cost
中,您将其添加到
int
s中。您可能需要先使用
int()
对其进行转换。正如其他人间接提到的,缩进已关闭。您可以在函数中使用
raw\u input
。您可以在函数中使用
raw_input
返回的字符串。但是,如果你想对输入数据进行算术运算,你需要使用
int()
float()
将其转换为数字。伙计们,我对编码有点陌生,所以请原谅我的许多错误。。。在我最初的代码中,我的缩进很好,但是在这里写它可能会犯一些错误。。。否则,谢谢你的帮助:)我没有看到问题中写的错误。但是您有几个问题,例如缩进和跨越多行的
return
语句。在这段代码中,您没有在函数中使用
raw\u input
raw\u input
返回
str
。但是在
trip\u cost
中,您将其添加到
int
s中。您可能需要先使用
int()
对其进行转换。正如其他人间接提到的,缩进已关闭。您可以在函数中使用
raw\u input
。您可以在函数中使用
raw_input
返回的字符串。但是,如果你想对输入数据进行算术运算,你需要使用
int()
float()
将其转换为数字。伙计们,我对编码有点陌生,所以请原谅我的许多错误。。。在我最初的代码中,我的缩进很好,但是在这里写它可能会犯一些错误。。。否则,谢谢你的帮助:)