mardi 29 novembre 2022

How to validate random choice within Python?

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.




Aucun commentaire:

Enregistrer un commentaire