简体   繁体   English

TypeError:“>”在“str”和“int”的实例之间不受支持 - Python

[英]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.

相关问题 Python:类型错误:“str”和“int”实例之间不支持“&lt;” - Python: TypeError: '<' not supported between instances of 'str' and 'int' Python-&#39;str&#39;和&#39;int&#39;的实例之间不支持&#39;TypeError:&#39;&lt;=&#39; - Python - 'TypeError: '<=' not supported between instances of 'str' and 'int'' TypeError:在 Python 中的“str”和“int”实例之间不支持“&gt;” - TypeError: '>' not supported between instances of 'str' and 'int' in Python 循环遍历字典和 TypeError:在“str”和“int”的实例之间不支持“&gt;=”-Python - Looping through dictionary and TypeError: '>=' not supported between instances of 'str' and 'int' - Python Python。 类型错误:“str”和“int”的实例之间不支持“&lt;” - Python. TypeError: '<' not supported between instances of 'str' and 'int' python 3 TypeError:if语句中的“ int”和“ str”的实例之间不支持“&gt;” - python 3 TypeError: '>' not supported between instances of 'int' and 'str' in an if statment 排序-TypeError:&#39;int&#39;和&#39;str&#39;的实例之间不支持&#39;&lt;&#39;,Python - Sorting - TypeError: '<' not supported between instances of 'int' and 'str' , Python Python 的新功能 - 类型错误:“str”和“int”实例之间不支持“&lt;” - New to Python - TypeError: '<' not supported between instances of 'str' and 'int' “TypeError: &#39;&gt;&#39; 在 &#39;int&#39; 和 &#39;str&#39; 的实例之间不受支持”最大值 - "TypeError: '>' not supported between instances of 'int' and 'str'" in max Paramiko TypeError:'int'和'str'的实例之间不支持'&lt;' - Paramiko TypeError: '<' not supported between instances of 'int' and 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM