Python 蟒蛇询问游戏

Python 蟒蛇询问游戏,python,Python,我正在做一个问题游戏(大约20个问题),但我希望我的程序只问一个问题一次。我曾尝试使用enumerate为ques中的每个字符串指定一个值,然后有一个if语句,表示if I=I:I!=我希望这会把i的值改成其他值,这样它就不会重复这些问题了,但那是行不通的。任何帮助都会很好,这是我第一次编程一个问题游戏,我对它有很高的期望,只要我能让它稳定下来 import sys, random keepGoing = True ques = ['What does it eat?', 'H

我正在做一个问题游戏(大约20个问题),但我希望我的程序只问一个问题一次。我曾尝试使用enumerate为ques中的每个字符串指定一个值,然后有一个if语句,表示if I=I:I!=我希望这会把i的值改成其他值,这样它就不会重复这些问题了,但那是行不通的。任何帮助都会很好,这是我第一次编程一个问题游戏,我对它有很高的期望,只要我能让它稳定下来

import sys, random
keepGoing = True
ques = ['What does it eat?', 
        'How big is it?', 
        'What color is it?', 
        'How many letters are in it?',
        'Does it have scales?',
        'Does it swim?',
        'How many legs does it have?'
        ]

ask = raw_input("Want to play a game?")
while keepGoing:
    if ask == "yes":
        nextQ = raw_input(random.choice(ques))
    else:
        keepGoing = False

我建议在你的问题列表中使用
random.sample
;它将获得所需数量的随机问题,无需重复

下面是一些经过清理的代码:

# assumes Python 2.x
from random import sample
from time import sleep

NUM_QUESTIONS = 4

questions = [
    'What does it eat?', 
    'How big is it?', 
    'What color is it?', 
    'How many letters are in it?',
    'Does it have scales?',
    'Does it swim?',
    'How many legs does it have?'
    # etc
]

def get_yn(prompt):
    while True:
        val = raw_input(prompt).strip().lower()
        if val in {'y', 'yes'}:
            return True
        elif val in {'n', 'no'}:
            return False

def play():
    for q in random.sample(questions, NUM_QUESTIONS):
        print(q)
        sleep(2.)

def main():
    while True:
        play()
        if not get_yn("Play again? "):
            break

if __name__=="__main__":
    main()

我建议在你的问题列表中使用
random.sample
;它将获得所需数量的随机问题,无需重复

下面是一些经过清理的代码:

# assumes Python 2.x
from random import sample
from time import sleep

NUM_QUESTIONS = 4

questions = [
    'What does it eat?', 
    'How big is it?', 
    'What color is it?', 
    'How many letters are in it?',
    'Does it have scales?',
    'Does it swim?',
    'How many legs does it have?'
    # etc
]

def get_yn(prompt):
    while True:
        val = raw_input(prompt).strip().lower()
        if val in {'y', 'yes'}:
            return True
        elif val in {'n', 'no'}:
            return False

def play():
    for q in random.sample(questions, NUM_QUESTIONS):
        print(q)
        sleep(2.)

def main():
    while True:
        play()
        if not get_yn("Play again? "):
            break

if __name__=="__main__":
    main()

这样做:

for question in random.shuffle(ques):
    #your code here, for each question

顺便说一句,如果您编写的代码是原样的,那么它会生成一个无止境的循环。尝试重新格式化它。

执行以下操作:

for question in random.shuffle(ques):
    #your code here, for each question

顺便说一句,如果您编写的代码是原样的,那么它会生成一个无止境的循环。尝试重新格式化它。

谢谢,我更改了它!谢谢,我改了@阿甘:哦,来吧。。。这真的很难,我就是那么好;-)哈哈,谢谢你的作品像一个魅力,我你也给了我这么多的代码学习以及!我做了一个小小的改变,并没有将NUM_问题设置为某个数字,而是将其设置为和我的问题列表的长度相等:)@Gumpy:若要这样做,您最好使用
random.shuffle()
@阿甘:哦,来吧。。。这真的很难,我就是那么好;-)哈哈,谢谢你的作品像一个魅力,我你也给了我这么多的代码学习以及!我做了一个小小的改变,并没有将NUM_问题设置为某个数字,而是将其设置为和我的问题列表的长度相等:)@Gumpy:若要这样做,您最好使用
random.shuffle()