0

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.

1
  • 2
    A small hint to get you started: Print out some more information (e.g. print(choices) print(inp) and so on...) while you are searching the bug. And read something about elif Commented Nov 29, 2022 at 8:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.