首页 > 解决方案 > 来自用户的输入未正确与数组进行比较

问题描述

我需要为我正在编写的程序实现一些代码(我的第一个代码项目,还不太擅长编程),并且我与用户输入和数组的比较不起作用。它旨在检查用户输入选项是否存在于数组中并将输入添加到变量中,但它一直在打印“玩家不在这个团队中,再试一次”。

    while first_bowl == False:
        bowler = input("Who is bowling first?: ")
        if bowler == Team1:
            first_bowl = True
            print('first bowler is {bowler}')
        elif bowler != Team1:
            print("Player not on this team, try again")

任何帮助都会很棒!

标签: pythonarrays

解决方案


使用in运算符检查项目是否在列表中:

while first_bowl == False:
  bowler = input("Who is bowling first?: ")
  if bowler in Team1:
    first_bowl = True
    print('first bowler is {bowler}')
  else:
    print("Player not on this team, try again")

推荐阅读