I am creating a web-scraping tool for some gift cards, and I have stored the name of each gift card (which is a string, i.e “PSN”) as a variable. So for example, P = “PSN”
I have a user input asking them to put in the name of the card they’re after, but I want it to be restricted to the variables I have already defined, so that if they type a name other than the cards I have already allowed for, it will return an error and ask them to type the name again.
How can I go about this?
Here is the code I’ve written so far:
JavaScript
x
13
13
1
#Stored strings for each card
2
G = "Google".casefold()
3
P = "PSN".casefold()
4
X = "Xbox".casefold()
5
I = "iTunes".casefold()
6
N = "Nintendo".casefold()
7
A = "Amazon".casefold()
8
S = "Steam".casefold()
9
10
Card_Name = input('Enter Card Name:')
11
cardnameanswer = G, P, X, I, N, A, S
12
Card_Name = cardnameanswer
13
Advertisement
Answer
Like this:
JavaScript
1
14
14
1
G = "Google".casefold()
2
P = "PSN".casefold()
3
X = "Xbox".casefold()
4
I = "iTunes".casefold()
5
N = "Nintendo".casefold()
6
A = "Amazon".casefold()
7
S = "Steam".casefold()
8
9
while True: # Keeps looping unless specified
10
Card_Name = input('Enter Card Name:')
11
if Card_Name in [G, P, X, I, N, A, S]: # If the card name entered is amoung the valid names
12
break # Break out of the loop, without continuing the rest of the loop
13
print('Invalid card name.') # Only prints if the user haven't broke out of the loop
14