I've been trying to make this dumb little program that spits out a random quote to the user from either Kingdom Hearts or Alan Wake (both included in .txt files) and I've hit a snag. I've made the program select a random quote from either of the text files (residing in lists) and to finish I just need to validate whether the user input matches the random selection.
import os
import sys
import random
with open(os.path.join(sys.path[0], "alanwake.txt"), "r", encoding='utf8') as txt1:
wake = []
for line in txt1:
wake.append(line)
with open(os.path.join(sys.path[0], "kh.txt"), "r", encoding='utf8') as txt2:
kh = []
for line in txt2:
kh.append(line)
random_kh = random.choice(kh)
random_wake = random.choice(wake)
choices = [random_kh, random_wake]
quote = random.choice(choices)
print(quote)
print("Is this quote from Kingdom Hearts or Alan Wake?")
inp = input().lower()
This is what I've got so far. I did try something like:
if quote == choices[0] and inp == "kingdom hearts":
print("Correct!")
if quote == choices[1] and inp == "alan wake":
print("Correct!")
else:
print("Incorrect")
But found that it just always printed as incorrect. Any help would be appreciated! I'm very new to programming.
CodePudding user response:
You are working with more than 1 if-statement this means that the programm is gonna check both of them individually also, check the first one if is correct is going to print 'correct' then is gonna check the next if-statement and if this one is false is gonna print "Incorrect", try doing this
if quote == choices[0] and inp == "kingdom hearts":
print("correct")
elif quote == choices[1] and inp == "alan wake":
print("correct")
else:
print("incorrect")
Here you check all the option and when one of them is correct it stop comparing and print the msg.