-1

The part where I am troubled is in my function validator. To be honest, this is my first time using functions so I am well aware that it isn't as compact as it could be but at the minute, I am more focused on making it work than anything. I tried swapping the "or"s for "and"s, but that didn't work either so I am at a roadblock on what to do. It is supposed to only exit if the user types something random but I typed it correctly and it still exited multiple times. FYI, ind.txt contains 5 words which are hi, hello, bye, goodbye, hey, each one on a new line and without the commas obviously.

from sys import exit

def open_file(file):
    f = open(file)
    open_file.content = f.read()
open_file('ind.txt')
words = open_file.content

word1, word2, word3, word4, word5 = words.split()
wordlist = [word1, word2, word3, word4, word5]

def validator():
    if guess != word1 or word2 or word3 or word4 or word5:
        print("Try inputting an option instead")
        exit()
    elif guess == word1 or word2 or word3 or word4 or word5:
        pass
validator()

1 Answers1

0

Changing the validation function makes it work.

change:

def validator():
    if guess != word1 or word2 or word3 or word4 or word5:
        print("Try inputting an option instead")
        exit()
    elif guess == word1 or word2 or word3 or word4 or word5:
        pass

to:

def validator():
    if guess not in wordlist:
        print("Try inputting an option instead")
        exit()
    elif guess in wordlist:
        pass

or and and conditions have to be specified individually. For example, if guess != word1 or guess != word2 or guess != word3. Python has an easier method to look in lists, so you don't have to compare against each word.

H. Gandhi
  • 36
  • 3