python - 来自用户的输入未正确与数组进行比较
问题描述
我需要为我正在编写的程序实现一些代码(我的第一个代码项目,还不太擅长编程),并且我与用户输入和数组的比较不起作用。它旨在检查用户输入选项是否存在于数组中并将输入添加到变量中,但它一直在打印“玩家不在这个团队中,再试一次”。
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")
任何帮助都会很棒!
解决方案
使用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")
推荐阅读
- mysql - 在 MongoDB 和 MySQL 数据库之间创建关系
- rust - 编写一个特征来使用多个数据库后端给了我“错误[E0597]:`connection`的寿命不够长”
- jquery - Owl-Carousel with Data Attribute not working
- pandas - 将非结构化 CSV 文件读入 pandas 数据框
- python - 错误 - b'sasl_client_start (-4) SASL(-4) 中的错误:没有可用的机制:无法找到回调:2'
- python - 取最后 N 天的最小值
- javascript - 执行 UI 块时抛出异常
- sql-server - SQL Server - sp_verify_job @name 已存在
- erlang - 获取回调的表示
- swiftui - SwiftUI:WKWebView 有没有办法在 WKWebView 完成加载后调用函数?