Python - 列出检查某些内容是否已经随机化

时间:2015-11-19 17:05:04

标签: python list

好的,所以我在python中创建一个程序,基本上它是一个针对手机用户的故障排除程序,它现在非常基础,我正处于这样一种情况,即我要创建一个列表来查询问题。

我将使用随机模块从故障排除问题列表中随机化一个字符串,但我不希望它将列表中的第一个问题随机化,然后再次列表中的第一个问题。

所以真正的问题;我如何检查随机化字符串是否已经被随机化,如果有,我希望我的程序从列表中随机化另一个字符串,如果已经说过,那么随机化另一个,如果不使用该字符串,等等。

注意:这个程序没有接近完成,我现在就开始这样了,所以我在最后调用函数,所以我可以在不同的时间运行程序,看它是否有效。

import random

Questions = [
            'Has your phone gotten wet?', 'Have you damaged your screen?', 'Is the phone at full battery?',
            'Has your phone been dropped?', ' Does the mobile phone turn itself off?', 'Does the device keep crashing',
            'Does the phone keep freezing?', 'Can you not access the internet on your phone?', 'Is the battery draining quickly?',
            'Can you not access certain files on your phone?'
            ]
Solutions = [
            'Put your mobile phone inside of a fridge, it sounds stupid but it should work!', 'Try turning your device on and off',
            'Visit your local mobile phone store and seek help'
        ]
def PhoneTroubleshooting():
    print('Hello, welcome to the troubleshooting help section for your mobile phone.\n'
            'This troubleshooting program is going to ask you a series of questions, to determine the issue with your device.')
    answer = print(random.choice(Questions))
    if answer == 'yes':
            print('Okay, I have a solution', random.choice(Solutions))

    else: print('Okay, next problem')

PhoneTroubleshooting()

1 个答案:

答案 0 :(得分:3)

您应该使用shuffle随机化整个列表,然后对其进行迭代,而不是一次选择一个随机元素。即。

random.shuffle(Questions)   # This shuffles `Questions` in-place

print(Questions[0])
...

但请注意,您可能希望保持两个列表的协调 - 即您仍然希望您的答案与您的问题相符,因此您应该将索引随机化而不是值:

inds = range(len(Questions))
random.shuffle(inds)
print(Questions[inds[0]])
...
print(Answers[inds[0]])