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

问题描述

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

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