Question1
WordArray=[]# global array to store words read from file
NumberWords=0# global variable to store num of word can be made from main word
List=[]
def ReadWords(fileName):
global WordArray, NumberWords, List
NumberWords=0
with open(fileName,'r') as f:
#use readlines() to store as a list, then access it
List=[lines.strip() for lines in f.readlines()]
WordArray.insert(0,List[0])
for i in range(1,(len(List))):#len=9, 9-1=8
WordArray.insert(i,List[i])
NumberWords+=1
#print('debug log:' , List)
Play()
def Play():
global WordArray, NumberWords,List
print(' The main word is: ',WordArray[0])#output main word from array
print(' Number of words: ',NumberWords)
corr=0
New=[]
while True:
enter=input('Please enter words, if you wish to stop then enter no: ').lower()
if enter=='no':
break
Found=False
for i in range(1, len(List)):#start checking from index 1
if List[i]== enter:#NonType object has no attribute strip()
print('This is a correct answer')
corr+=1
List[i] = None #marked as used
Found=True
#break out of for loop to while True
break#stop checking once found correct answer
if not Found:
print('This is not a correct answer.')
#ouput precentage of answers when entered no
#to prevent zero division
if NumberWords > 0:
percentage = (corr / NumberWords) * 100
else:
percentage = 0
print(f'This is the percentage of correct answers: {percentage:.2f}%')
#output all answers user did not enter
## for y in range(1,len(List)):
## if List[y]!=None:
## New.append(List[y])
New=[word for word in List[1:] if word is not None]
if New:
print(f'These are the answers that you did not enter: {", ".join(word.strip() for word in New)}', end='')
else:
print("You answered all correctly!")
def main():
global WordArray, NumberWords
enter=input('Please enter easy, medium, or hard:').lower().strip()
while enter not in ['easy', 'medium','hard']:
print('Please only enter the stated word.')
enter=input('Please enter again').strip().lower()
if enter=='easy':
ReadWords('Easy.txt')
elif enter=='medium':
ReadWords('Medium.txt')
elif enter=='hard':
ReadWords('Hard.txt')
main()
Outputs:
Please enter easy, medium, or hard:easy
The main word is: house
Number of words: 14
Please enter words, if you wish to stop then enter no: she
This is a correct answer
Please enter words, if you wish to stop then enter no: out
This is not a correct answer.
Please enter words, if you wish to stop then enter no: no
This is the percentage of correct answers: 7.14%
These are the answers that you did not enter: hues, hose, hoes, shoe, sou, ohs, ose, oes, sue, use, hue, hoe, hes
Question2_J24.py