Skip to main content
 首页 » 编程设计

python之简单的python程序帮助!

2025年05月04日17rubylouvre

该程序应该计算给定日期低于 60 度的度数,然后创建度数的运行总和。 count 等于 60 以下的度数之和。但是,当我运行它时,出现此错误:

cool = 60 - temp 

TypeError: 不支持的操作数类型 -: 'int' 和 'str'

关于它为什么这样做的任何想法?谢谢!

def cold_days(): 
 
    temp = eval(input("What is the temperature? ")) 
    count = 0 
 
    if temp < 60: 
        while temp !="quit": 
 
            temp = eval(input("What is the temperature? ")) 
            cool = 60 - temp 
            count = count + heat 
        print(count) 
    else: 
        print("you have no cold days") 

请您参考如下方法:

您需要将 temp 转换为 int:

... 
 
try: 
    temp = int(temp) 
except TypeError: 
    # Handle invalid integer 
    print("%s is not a valid integer." % temp) 
    sys.exit(1) 
 
...