Jacob Fox Jacob Fox - 5 months ago 6x
Python Question

Input not working

I've been creating a studying program for learning japanese using python and tried condensing and randomizing it butnow it doesnt do the input,i have analyzed it multiple times and cant find any reason here is what i have for it so far,any suggestions would be appreciate

import sys
import random
start = input("Are you ready to practice Japanese Lesson 1? ")
if start.lower() =="yes":
print("Ok Let's Begin")
questiontimer = 0
while questiontimer<10:
questiontimer = (int(questiontimer) + 1)
WordList = ["konnichiwa"]
rand_word = random.choice(WordList)
if rand_word == "konnichiwa":
answer = input("Question "+ questiontimer +":Say hello In japanese.")
if rand_word == answer.lower():
elif randword!= answer.lower():
print("Incorrect, the answer is Konnichiwa")

this is as condensed as i could get it to reproduce the problem after

print("Ok Let's Begin")

it is supposed to pick a random string from the list then ask for input based on which word it is right now it has only one string in the list but still does not print what is in the input or allow input for the answer


Some slight improvements from the other answer to condense some code and the desired behavior (I think) you were actually trying to achieve using your original word list, since using just a single word would be redundant here.

You were grabbing a random word, dict's are unordered unless you use an ordered dict of course. So, iterating over the dict's items here is more or less a random permutation of the questions.

Not sure what you were really trying to achieve with the questiontimer so I just ditched it here for a simplistic q and a style program.

Edit: To add explanation for "fancy" parts of code

When we wrap an iterable inside of enumerate it simply returns a tuple containing the current number we're at in the iterable -- It starts at zero, everything in python is base zero. Forloops, indexing, you name it -- and what would have originally been returned by the iterable if you had just used a simple for loop.

So, enumerate as you see in the documentation just returns this tuple of count / item in iterable. Thus, when we do enumerate(qa.items()) we have a tuple containing the count and the key / value pair (which is also a tuple) as returned by iterating over qa.items(). So, we use for i, (k, v) ... to iterate over these items. i is the count here it is the first item in the tuple returned by enumerate and (k, v) is the tuple containing the key / value pair from the dictionary. Note: We have to do i, (k, v) not i, k, v. This would be attempting to unpack 3 seperate items from what's returned by enumerate instead of just the two it contains since it is in fact a 2-tuple. (A tuple contaning two elements)

In fact, should we try to use i, k, v it will throw the following error for this very reason:

     for i, k, v in enumerate(qa.items()):
 ValueError: need more than 2 values to unpack

%s and %d are simply string formatters. You can use these to format different types of data structures into strings. %s is for putting strings into other strings. %d is for putting integers into strings. %f is for putting floats... so on and so forth.

#initial two vars for keeping a tally of incorrect and correct answers by the user.
correct = 0
incorrect = 0

#Just a dict to hold the phrase and the english equivalent in a key / value pair
qa = {"konnichiwa": 'hello',
      "ohayo gozaimasu": "good-morning",
      "oyasuminasai": "goodnight",
      "sayonara": "goodbye",
      "dewa mata": "see you" ,
      "dewa mata ashita": "see you tomorrow",
      "dewa mata raishu": "see you next week",

start = input("Are you ready to practice Japanese Lesson 1?")
if start.lower() == "yes":

    for i, (k,v) in enumerate(qa.items()):
         answer = input("Question %d: Say %s in Japanese.\n" % (i, v))
         if answer != k:
               incorrect += 1
               print("Incorrect the answer is: %s" % k)
               correct += 1
    print("Num correct: %d\nNum incorrect: %d" % (correct, incorrect))
    print("Maybe next time.")