简体   繁体   English

如何检查列表中的 integer 中是否有内容

[英]How do a check if something is in an integer that is in a list

This is some of my code: (I defined the variables in the list "crates" by choosing a random number for each of them by the way)这是我的一些代码:(我通过为每个变量选择一个随机数来定义列表“板条箱”中的变量)

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

I'm trying to check if the number the user inputted is the number writing of the variables in the list "crates".我正在尝试检查用户输入的数字是否是“板条箱”列表中变量的数字写入。 How do I do this?我该怎么做呢? For example, the person inputs the number 17, I need to make the computer figure out that "17" is seventeen, then remove it from the list.例如,人输入数字 17,我需要让计算机计算出“17”是 17,然后将其从列表中删除。 How do I do this?我该怎么做呢? I also need to keep 'choicecrate' as an integer.我还需要将“choicecrate”保留为 integer。 This may be an easy question, but I'm a beginner.这可能是一个简单的问题,但我是初学者。

Since the list is in sequence, one, two, three etc. You can simply check if the number the user entered is valid by comparing it to the length of the list.由于列表是按顺序排列的,一、二、三等。您可以通过将其与列表的长度进行比较来简单地检查用户输入的数字是否有效。

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!")

code:代码:

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"]
keys = list(range(1,27))
dic = dict(zip(keys,crates))
for _ in range(3):
    choicecrate = int(input("Which crate do you think is your lucky crate? "))
    if choicecrate in dic.keys():
        print("The choicecrate in crates!")
        del dic[choicecrate]
    else:
        print("The choicecrate not in crates!")

result:结果:

Which crate do you think is your lucky crate? 0
The choicecrate not in crates!
Which crate do you think is your lucky crate? 17
The choicecrate in crates!
Which crate do you think is your lucky crate? 17
The choicecrate not in crates!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM