输入不起作用(Input not working)

编程入门 行业动态 更新时间:2024-10-27 06:26:22
输入不起作用(Input not working)

我一直在创建一个学习日语使用python的学习程序,并尝试冷凝和随机化它但是它没有做输入,我已经多次分析它并且无法找到任何理由这里是我迄今为止所拥有的,任何建议都会要欣赏

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(): print("Correct!") elif randword!= answer.lower(): print("Incorrect, the answer is Konnichiwa")

这就像我可以让它重现问题后一样浓缩

print("Ok Let's Begin")

它应该从列表中选择一个随机字符串然后根据它现在请求输入它现在它在列表中只有一个字符串但仍然不打印输入中的内容或允许输入答案

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(): print("Correct!") 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

最满意答案

从另一个答案中略微改进来压缩一些代码和所需的行为(我认为)你实际上试图使用你的原始单词列表来实现,因为在这里只使用一个单词将是多余的。

你抓了一个随机的单词,dict是无序的,除非你当然使用有序的dict。 因此,在这里迭代dict的项目或多或少是问题的随机排列。

不确定你真正试图通过questiontimer实现什么,所以我在这里放弃了一个简单的q和风格程序。

编辑:添加“花哨”部分代码的说明

当我们在枚举中包装一个iterable时,它只返回一个元组,其中包含我们在迭代中的当前数字 - 它从零开始,python中的所有内容都是基数为零。 Forloops,索引,你命名它 - 如果你刚刚使用了一个简单的for循环,那么iterable最初会返回什么。

因此,如文档中所示, enumerate只是在iterable中返回count / item的这个元组。 因此,当我们enumerate(qa.items())我们有一个包含count和键/值对(也是一个元组)的元组,通过迭代qa.items()返回。 因此,我们使用for i, (k, v) ...迭代这些项目。 i是这里的计数它是枚举返回的元组中的第一项,而(k, v)是包含字典中的键/值对的元组。 注意:我们必须做i, (k, v)而不是i, k, v 。 这将尝试从enumerate返回的内容中解压缩3个单独的项目,而不仅仅是它包含的两个项目,因为它实际上是一个2元组。 (包含两个元素的元组)

事实上,如果我们尝试使用i, k, v它会抛出以下错误,原因如下:

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

%s和%d只是字符串格式化程序。 您可以使用它们将不同类型的数据结构格式化为字符串。 %s用于将字符串放入其他字符串中。 %d用于将整数放入字符串中。 %f用于放置浮动......依此类推。

#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) else: correct += 1 print("Correct.") print("Num correct: %d\nNum incorrect: %d" % (correct, incorrect)) else: print("Maybe next time.")

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) else: correct += 1 print("Correct.") print("Num correct: %d\nNum incorrect: %d" % (correct, incorrect)) else: print("Maybe next time.")

更多推荐

本文发布于:2023-07-27 17:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   Input   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!