[英]TypeError: '>' not supported between instances of 'str' and 'int' - Python
I'm trying to save user inputs into a file but I keep getting a TypeError.我试图将用户输入保存到一个文件中,但我不断收到 TypeError。 How do I fix it
我如何解决它
from passenger import *
file = open("passenger.txt", 'w')
continue_record = True
while continue_record:
record = input("\nRecord passenger (y/n): ")
if record == 'n':
continue_record = False
else:
distance, name, passenger_type = input_passenger()
total_fare = compute(distance)
get_receipt = receipt(name,passenger_type, distance, total_fare)
file.write(get_receipt)
file.write("\n")
file.close()
This is my input_passenger function:这是我的 input_passenger function:
def input_passenger():
global distance
global passenger_name
global passenger_type
passenger_name = input("Enter your name: ")
distance = float(input("Enter distance: "))
passenger_type = input("Enter type of passenger: ")
return passenger_name, distance, passenger_type #I tried returning them but it gives me this error
Error:错误:
line 14, in <module>
total_fare = compute(distance)
line 29, in compute
if distance > 0:
TypeError: '>' not supported between instances of 'str' and 'int'
This is my code in compute function这是我在计算中的代码 function
def compute(distance):
global fare
fare = 0
if distance > 0:
fare += 3 * 50
fare += (distance - 3) * 4.5
else:
fare = round(distance * 50, 2)
return fare
I tried converting it, but it still the error我尝试转换它,但它仍然是错误
You are returning the elements in a different order of unpacking them.您正在以不同的解包顺序返回元素。
return passenger_name, distance, passenger_type
You unpack as the following (note that distance is actually the passenger_name).您解压如下(注意 distance 实际上是 passenger_name)。
distance, name, passenger_type = input_passenger()
You want你要
name, distance, passenger_type = input_passenger()
But yes like what others have said you likely want to avoid globals whenever you can.但是是的,就像其他人所说的那样,您可能希望尽可能避免使用全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.