def quiz():
Birds = {'Eagle', 'Parrot', 'Woodpecker', 'Sparrow', 'Pigeon'}
Furniture = {'Chair', 'Bench', 'Sofa', 'Closet', 'Table'}
Fruit = {'Apple', 'Cherry', 'Pear', 'Grape', 'Strawberry'}
Plants = {'Sunflower', 'Cactus', 'Moss', 'Brambles', 'Wheat'}
print("1. Bird \n2. Chair \n3. Apple \n4. Grass")
question = int(input("which one is a fruit?: "))
if question == 1:
print("Thats wrong! But other kinds of birds are:", ','.join(Birds))
if question == 2:
print("Thats wrong! But other kinds of furniture are:", ','.join(Furniture))
if question == 3:
print("That's Correct! Other kinds of fruit are:", ','.join(Fruit))
if question == 4:
print("Thats wrong! But other kinds of plants are:", ','.join(Plants))
input("Press Enter to start over")
quiz()
quiz()
You would use a Dictionary if your indexes have a special meaning
besides just positional placement.
The immediate example that comes to mind is storing an id column and
an int column in a database. For example, if you have a [person-id]
column and a [personal-pin] column, then you might bring those into a
Dictionary. This way pinDict[person-id] gives you a PIN, but
the index is meaningful and not just a position in a List.
But really, any time you have two related lists of integers, this
could be an appropriate data structure.
你真的可以使用字典,我为你做了一个快速的文件,这样你就可以了解一切的反应。注意:我这样做是为了可读性,对python了解得越多,编写代码的效率就越高
首先定义函数,以便重新调用。然后词汇表就被创建了,不需要把它们放在函数中,但是再放一次;可读性
有些选项是打印的
\n
插入一个enter
。这样你就不会有4个打印语句在彼此下面了。 然后问一个问题int(input("text"))
确保给出的答案是整数,而不是基数10然后是一个简单的
if question == 1:
,它告诉程序当question = int(input())
为1时要做什么希望你觉得这有帮助,这就是你的意思。如果没有,请随意评论,我会尽力帮助你
一些来自https://softwareengineering.stackexchange.com/questions/139052/dictionary-vs-list的词典:
相关问题 更多 >
编程相关推荐