从用户输入获取数据类型转换错误

问题描述

此代码存在问题,我尝试更改几乎每个部分的数据类型。我看不到错误在哪里。我知道这段代码可能有很多问题。我对编程很陌生。

team_1 = input(str("enter the name of team one "))
team_1_avg_goals = input(str("enter the average goals for " + team_1))
team_1_win_percentage = input(str("enter the win rate of " + team_1))
team_2 = input(str("enter the name of team two "))
team_2_avg_goals = input(str("enter the average goals for " + team_2))
team_2_win_percentage = input(str("enter the win percentage for " + team_2 ))

if team_1_win_percentage > team_2_win_percentage:
    print(team_1 + " should win")

else: print(team_2 + " should win")

total_goals = team_2_avg_goals + team_1_avg_goals/2

print(float(total_goals))

错误:

TypeError: unsupported operand type(s) for /: 'str' and 'int'

解决方法

您必须将team_1_avg_goals转换为int。您可以在输入行上完成此操作:

team_1_avg_goals = int(input("enter the average goals for " + team_1))
team_2_avg_goals = int(input("enter the average goals for " + team_2))

或此处:

total_goals = int(team_2_avg_goals) + int(team_1_avg_goals)/2

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...