在控制台中循环写数字

时间:2019-05-02 15:01:38

标签: python type-conversion

我正在做运动以训练自己,目前我被循环中的变量阻塞。

main.py

from interface import *
from function import *

player_number(choose_pseudo)

interface.py

list_players = []


def player_number(callback):
    players = int(input("Nombre de joueurs ? [1 à 4]\n"))
    callback(players)


def choose_pseudo(p):
    for i in range(p):
        player = input("Joueur " + i + " choisissez un pseudo ?\n").lower()
        list_players.append(player)

我想在第二个功能中循环输入通过控制台输入的玩家人数(所以是一个字符串)。

编辑:错误

Nombre de joueurs ? [1 à 4]
2
Traceback (most recent call last):
  File "/home/antoine/Projet/Python AP1/MarioParty/main.py", line 9, in <module>
    player_number(choose_pseudo)
  File "/home/antoine/Projet/Python AP1/MarioParty/function.py", line 6, in player_number
    callback(players)
  File "/home/antoine/Projet/Python AP1/MarioParty/function.py", line 11, in choose_pseudo
    player = input("Joueur " + i + " choisissez un pseudo ?\n").lower()
TypeError: must be str, not int

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

i是一个整数,因此您需要在input调用中将其转换为字符串:

player = input("Joueur " + str(i) + " choisissez un pseudo ?\n").lower()