我在Python 3.X程序中获得了TypeError,但我不知道如何更改它



我是Python和编程的新手我有,以帮助我更好地理解。但是,只要到达代码的这一部分时,就会给我这个错误。

  File "TeamSelector.py", line 12, in <module>
    print ("n You have been asigned to team number: " + str(teamSelection % teams))
TypeError: not all arguments converted during string formatting
PS C:usersworridocumentsdevelopmentpython>

这是我的完整代码

import sys
testInt = 64
teams = input("n How many teams are there? n")
print ("There are " + teams + " number of teams.")
totalPlayers = input ("n How many total players are there n")
print ("There are " + totalPlayers + " players to be selected into teams.")
teamSelection = input ("n What is your assigned number? n")
print ("n You have been asigned to team number: " + str(teamSelection % teams))

我尝试了很多事情,甚至有一个单独的变量来进行计算以将其作为字符串的一部分传递,但我以不同的方式查看了Google的不同方式,而我尝试过的任何方法都会产生相同的错误。我足够了解,我认为TypeError在将Float/Int传递到字符串上遇到问题,但是我似乎找不到一个.toString()功能,这是我尝试学习Java之前使用过的。如果有人能帮助我,我将最感激。谢谢。

此错误是由于您的 teamSelectionteams变量必须在服用模量之前将其转换为整数(模量运算符在数字上,而不是字符串)。当您使用input()函数时,您的输入将读为字符串。

可以通过更改以下代码的最后一行来解决这:

team_assignment = int(teamSelection) % int(teams)
print("n You have been asigned to team number: " + str(team_assignment))

始终使用snake_case命名您的变量。请参阅本文

teams = input("How many teams are there? n")
print("There are " + teams + " number of teams.")
total_players = input("How many total players are there n")
print("There are " + total_players + " players to be selected into teams.")
team_selection = input("What is your assigned number? n")
print("You have been assigned to team number: " + str(int(team_selection) % int(teams)))

否则

print(f"You have been assigned to team number: {str(int(team_selection) % int(teams))}")

相关内容

最新更新