Python-简单的随机选择器

时间:2014-09-25 10:36:37

标签: python random

我正在python中创建一个简单的游戏,它会问你问题,然后使用答案来填补故事中的空白。我想这样做,以便每次都以随机顺序提问,但我想提出所有问题。

我在Mac OSX上使用python 2.7.8,想知道我能做些什么才能得到这个结果?

提前感谢您的帮助!

以下代码如果有帮助:

while True: 

    print "Welcome to Consequences"
    print ' '
    print "Game is more fun in groups!"
    print''
    print "Group instructions :"
    print "Get in a circle and let each person enter one thing each without the rest of the group seeing    what they enter"
    print ' '
    print ' '
    print raw_input("Press enter to start")
    hide(str)

    nameb = raw_input("Enter a boys name then press enter ")

    hide(str)

    nameg = raw_input("Enter a girls name then press enter ")

    hide(str)

    place = raw_input("Enter a place that two people meet then press enter ")

    hide(str)

    saidtg = raw_input("Enter what the boy said to the girl then press enter ")

    hide(str)

    saidtb = raw_input("Enter what the girl said to the boy and then press enter ")

    hide(str)

    end = raw_input("Enter what happened in the end and then press enter ")

    hide(str)

    print "Here is your story:"
    print ' '
    print nameb, "met", nameg
    print "they met at", place
    print "then",nameb,"said to", nameg, saidtg
    print "then",nameg,"replied with", saidtb
    print "in the end", end

    raw_input("Press enter to start again")
    print ' '
    print ' '
    print ' '
    print ' '

1 个答案:

答案 0 :(得分:0)

您可以将问题存储在列表中,并使用字典来存储变量:

questions = [('Enter name of boy', 'nameb') , ('Enter place', 'place'),...]

然后洗牌并询问每个问题:

>>> import random
>>> random.shuffle(questions,random.random) #shuffle list in place
>>> d = {}
>>> for (q,v) in questions:
...    d[v] = raw_input(q)
...    #more stuff like hide(str)

然后代替:

print nameb, "met", nameg
print "they met at", place

你只需做

print d['nameb'], "met", d['nameg']
print "they met at", d['place']