KyleTalksAlot
KyleTalksAlot

Reputation: 21

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. How do I do this? I also need to keep 'choicecrate' as an integer. This may be an easy question, but I'm a beginner.

Upvotes: 1

Views: 60

Answers (2)

leaf_soba
leaf_soba

Reputation: 2518

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!

Upvotes: 1

user9321739
user9321739

Reputation:

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

Upvotes: 1

Related Questions