GotRiff
GotRiff

Reputation: 87

How can I make a variable equal more then one string - Python

I am working on a quiz game and when I get asked any question, i type in the right answer, which I defined in the a variable that holds that correct answer but it says it is wrong if it isn't the first word for that variable (In this case, Metallica). Here is what my code looks like.

q1answer = "Metallica" or "metallica" or "Slayer" or "slayer" or "Anthrax" or "anthrax" or "Megadeth" or "megadeth"

answerinput = str(input("name one of the 'Big Four' metal bands'"))

if answerinput == q1answer:
    print ("You got the right answer!")
else:
    print ("That is the wrong answer...")

Now, if I was to type in 'Metallica' it would be correct because it is the first word for the q1answer variable. But, if I type metallica, Slayer, slayer, Anthrax, anthrax, megadeth or Megadeth, I would get the wrong answer text. Is there a way for all of these strings to be working for this one variable if i was to type anything but the first word in that variable? I'm a rookie in python so any help would be great.

Upvotes: 1

Views: 4532

Answers (2)

sberry
sberry

Reputation: 132098

Change your code like this:

q1answer = ("metallica", "slayer", "anthrax", "megadeth")
...

if answerinput.lower() in q1answer:
    ...

Upvotes: 1

Ry-
Ry-

Reputation: 225125

Put them in a set and test for a correct answer using in instead:

q1answer = {"Metallica", "metallica", "Slayer", "slayer", "Anthrax", "anthrax", "Megadeth", "megadeth"}
...
if answerinput in q1answer:

Right now, your series of ors is just resulting in the first one - "Metallica". You can also stop duplicating every name for case-insensitivity and instead use lower:

if answerinput.lower() in q1answer:

Upvotes: 3

Related Questions