Python琐事游戏

时间:2013-10-10 13:23:47

标签: python

我想使用2个数组或列表进行琐事游戏。如何将项目从一个列表链接到另一个列表?我有这段代码:

import random

mylist = ['A','B','C']
answer = ['1','2','3']
value = random.choice(mylist)
print (value)
input()
mylist.remove(value)
value = random.choice(mylist)
mylist.remove(value)
print (value)
input()
value = random.choice(mylist)
print(value)
mylist.remove(value)

我知道如何从mylist(问题)中随机选择一个变量但是如何将A表示为1并让用户输入1才能正确?

如何将一个列表链接到另一个列表并进行琐事游戏?它需要提出问题,然后用户输入答案。如果正确的话,他们会得到一个观点,在4个问题后,它会询问他们是否想要再次发挥,然后是3个问题的4个新问题,并且必须保持他们正确的数量得分。

2 个答案:

答案 0 :(得分:4)

这个程序正好处于可接受的列表/列表之间以及更好地用类写的边界之间。如果它有可能会增长,你可能想要将它重新组织为面向对象程序(OOP),如下所示:

import sys
import random

class Question(object):
    def __init__(self, question, answer, options):
        self.question = question
        self.answer = answer
        self.options = options

    def ask(self):
        print self.question + "?"
        for n, option in enumerate(self.options):
            print "%d) %s" % (n + 1, option)

        response = int(sys.stdin.readline().strip())   # answers are integers
        if response == self.answer:
            print "CORRECT"
        else:
            print "wrong"

questions = [
    Question("How many legs on a horse", 4, ["one", "two", "three", "four", "five"]),
    Question("How many wheels on a bicycle", 2, ["one", "two", "three", "twenty-six"]),

    # more verbose formatting
    Question(question="What colour is a swan in Australia",
             answer=1,
             options=["black", "white", "pink"]),    # the last one can have a comma, too
    ]

random.shuffle(questions)    # randomizes the order of the questions

for question in questions:
    question.ask()

这使得数据结构如何被使用的逻辑更接近于定义数据结构的位置。另外(解决您的原始问题),将问题与答案相关联很容易,因为它们不再位于单独的列表中。

没有动力不这样做,因为Python不需要额外的工作来创建课程。这与列表词典解决方案(基于它的基础)的长度大致相同,并且可以说更容易阅读,在您坐下来开始编程之前更接近您的想法。我经常在四行程序中使用类:OOP不一定非常重要。

编辑您可以像处理整数或字符串列表一样操纵类实例列表。例如,

del questions[0]

从列表中删除第一项

questions.append(Question("What is a new question", 1, ["This is."]))

在列表末尾添加了一个新问题,pop允许您将列表用作堆栈等。(有关详细信息,请参阅Python教程。)如果要使用list {{{{{{{ 1}}方法,而不是通过索引删除它(remove做什么),然后你需要告诉Python两个问题是什么意思相同。

尝试将这两种方法添加到课程中:

del

如果问题字符串相同,则第一个将 def __eq__(self, other): return self.question == other.question def __repr__(self): return "<Question: %s? at %0x>" % (self.question, id(self)) 个实例定义为相等。你可以扩展定义,同时要求他们的答案是相同的,但你明白了。它允许你这样做:

Question

删除马匹问题。

第二个在Python命令行上打印问题对象,这样你就可以看到在试验这些命令时你正在做什么。学习的最佳方式是通过反复试验,这使得反复试验更有效率。

答案 1 :(得分:2)

使用数组数组。

每个问题一个数组

在每个数组中,第一个元素是问题,第二个元素是正确答案的索引,剩余部分是可能的答案选项

我没有将这些问题随机化,因为你已经可以这样做了

import sys

questions=[
  ['How many legs on a horse',4, 1, 2, 3, 4 ,5],
  ['How many wheels on a bicycle',2,'one','two','three','twenty six'],
  ['What colour is a swan in Australia',1, 'black', 'white', 'pink']
  ]

for ask in questions:
    print ask[0]+'?'
    n = 1
    for options in ask[2:]:
        print "%d) %s" % (n,options)
        n = n + 1
    response = sys.stdin.readline().strip()
    if int(response) == ask[1]:
        print "CORRECT"
    else:
        print "wrong"

或者,根据评论中的建议(感谢Don)使用词典。

import sys

questions = [
  {
  'question': 'How many legs on a horse',
  'answer': 4,
  'options': [1, 2, 3, 4, 5]},
  {
   'question': 'How many wheels on a bicycle',
   'answer': 2,
   'options': ['one', 'two', 'three', 'twenty six']},

  {
   'question': 'What colour is a swan in Australia',
   'answer': 1,
   'options': ['black', 'white', 'pink']}
  ]

for ask in questions:
    print ask['question'] + '?'
    n = 1
    for options in ask['options']:
        print "%d) %s" % (n, options)
        n = n + 1
    response = sys.stdin.readline().strip()
    if int(response) == ask['answer']:
        print "CORRECT"
    else:
        print "wrong"

希望这会有所帮助