python - 如何检查某物是否在列表中的整数中
问题描述
这是我的一些代码:(我通过为每个变量选择一个随机数来定义列表“板条箱”中的变量)
crates = [one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, twentyone, twentytwo, twentythree, twentyfour, twentyfive, twentysix]
choicecrate = int(input("Which crate do you think is your lucky crate? "))
#insert figuring out if 'choicecrate' is 1, 2, 3, 4, etc. then removing it from the list
我正在尝试检查用户输入的数字是否是“板条箱”列表中变量的数字写入。我该怎么做呢?例如,人输入数字 17,我需要让计算机计算出“17”是 17,然后将其从列表中删除。我该怎么做呢?我还需要将“choicecrate”保留为整数。这可能是一个简单的问题,但我是初学者。
解决方案
由于列表是按顺序排列的,一、二、三等。您可以通过将其与列表的长度进行比较来简单地检查用户输入的数字是否有效。
crates = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive", "twentysix"]
choicecrate = int(input("Which crate do you think is your lucky crate? "))
if 1 <= choicecrate <= len(crates):
print("Input is in the list!")
推荐阅读
- android - 使用Kotlin显示存储在值/中的数组中的随机字符串?
- numpy - numpy 数组是在 spark 驱动程序还是执行程序上创建的?
- python - 我需要使用 python 在具有 144 列的数据集上创建 corr 矩阵
- c# - Powershell ASP.net 返回某些字段
- c# - 在 Visual Studio for Mac 8.6.2 中运行 C# 控制台应用程序时使用系统终端
- c - 为什么在此代码中使用 fgets 时会跳过用户输入?
- reactjs - 如何使用另一个选择的值在选择中进行过滤
- php - 将表单数据转发到@mail 帐户和 php 表单
- reactjs - 结果组件未正确更新
- javascript - 在android上的html输入字段上使用eventlistener的奇怪行为