![]() |
Simple mistake about for - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Simple mistake about for (/thread-28365.html) |
Simple mistake about for - Nomatter - Jul-15-2020 Hello , sorry for this simple mistake but I could not solve this. I am trying to text firstly english then turkısh words.Then they will come random and if I know correct ,correctWords will increase. I have simple mistake that ı can not see, So pls can u show me where I did mistake. import random list_words = [] turkısh_words= [] def writeWords(): print('Welcome to your english program.. To exit please write "exit"\n') while True: words = input('Your English word: ') if words == 'exit': break else: if words == "stop": print(list_words) break else: list_words.append(words) while True: answers = input('Your Turkısh word: ') turkısh_words.append(answers) break print(turkısh_words) print(f'number of total words is {len(list_words)}') def getWords(): writeWords() wrong_Words = [] correctWords = 0 print("Words are coming...\n") for i in range(1,len(turkısh_words)+l): get = random.choice(list_words) print(get) answer = input("Answer: ") if get == answer: correctWords += 1 else: wrong_Words.append(get) print(f'Number of correct answers: {correctWords}') print(wrong_Words) getWords()I have a mistake about for probably. I do not know what to do , Help me pls. Thank you. RE: Simple mistake about for - deanhystad - Jul-16-2020 There is no reason to follow break; with else. Break takes you out of the loop. All remaining code in the loop is inside an implicit else. def writeWords(): print('Welcome to your english program.. To exit please write "exit"\n') while True: words = input('Your English word: ') if words in ('exit', 'stop'): break list_words.append(words) answers = input('Your Turkısh word: ') turkısh_words.append(answers) print(list_words) print(turkısh_words) print(f'number of total words is {len(list_words)}')I can't see your mistake either. I type words and it keeps prompting me for more words. When I enter 'stop" it prints a list of words and I get an error for line 28. Is that the error? Is it the error that is caused by thinking lowercase L is a one? Or is the error that getWords doesn't do what you want? I kind of expected a quiz. The computer prints a random word and I am supposed to enter t he translation. Instead the code tells me the answer is correct if I type in the word that is displayed. Is that the error? All code longer than a few dozen lines is riddled with errors. This is especially true when you are learning how to program. Next time you have a question, please ask the question and provide as much detail as you can. RE: Simple mistake about for - Nomatter - Jul-16-2020 In fact there is no error but I could not make a good 'for loop'. I wanted to make = English word = yes, then Turkısh word = evet, When 'yes' will come random, when I will write 'evet'(in english 'yes') correctWords will increase. But there is a problem here writeWords() wrong_Words = [] correctWords = 0 print("Words are coming...\n") for i in range(1,len(turkısh_words)+l): get = random.choice(list_words) print(get) answer = input("Answer: ") if get == answer: correctWords += 1 else: wrong_Words.append(get) print(f'Number of correct answers: {correctWords}') print(wrong_Words)When I answers like this, yes = evet , it should be correct. Correct answer but, if I write this, it's being wrong. I mean if I wrote, yes = yes , it accepts it's true. But I wanted, yes = evet. Sorry probably I did not tell well. I will write in english then turkısh, when ı will answer in turkısh ; it will be correct but I could not do it. RE: Simple mistake about for - deanhystad - Jul-16-2020 This becomes an easy problem when you make a two direction translation dictionary. For each pair of words, add a Turkish to English entry and an English to Turkish entry. Then you can randomly select a key from the dictionary and ask for the translation. import random translator = {} turkish = [] english = [] def enter_translation(): print('Welcome to your english program.. To exit please write "exit"\n') while True: a = input('Your English word: ') if a in ('exit', 'stop'): break b = input('Your Turkısh word: ') english.append(a) # Don't need these lists turkish.append(b) translator[a] = b translator[b] = a def turkish_translation(): # To test English -> Turkish correct = 0 random.shuffle(english) for word in english: answer = input(word + ': ') if answer == translator[word]: correct += 1 print("Correct!") return correct def turkish_translation(): # To test Turkish -> English correct = 0 random.shuffle(turkish) for word in turkish: answer = input(word + ': ') if answer == translator[word]: correct += 1 print("Correct!") return correct def mixed_test(): # Randomly mix English and Turkish words correct = 0 words = list(translator.keys()) random.shuffle(words) for word in words: answer = input(word + ': ') if answer == translator[word]: correct += 1 print("Correct!") return correct enter_translation() print ("You got ", mixed_test(), "correct") RE: Simple mistake about for - Nomatter - Jul-16-2020 (Jul-16-2020, 02:21 AM)deanhystad Wrote: This becomes an easy problem when you make a two direction translation dictionary. For each pair of words, add a Turkish to English entry and an English to Turkish entry. Then you can randomly select a key from the dictionary and ask for the translation. Thank you so much. I was looking for dictionary to use. And u did it exactly how I wanted. Thank you so much. |