如何解决从用户输入获取数据类型转换错误
此代码存在问题,我尝试更改几乎每个部分的数据类型。我看不到错误在哪里。我知道这段代码可能有很多问题。我对编程很陌生。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。