2015-11-19 46 views
1

好了,所以我在Python中創建一個程序,基本上是手機用戶故障排除程序,這是截至目前非常基本的,我的情況下,我我正在爲要問的問題創建一個列表。的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() 

回答

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]]) 
+0

如果有一個1:解決方案和問題之間1的關係,我們也可以認爲贊成的名單(問題解決方案)元組。 – Jasper

+0

雖然我的代碼會如何工作?我不太確定我明白 – Thom9son

+0

於是,我嘗試過了,我不斷收到了「範圍」對象類型錯誤不支持@ Thom9son項目分配 – Thom9son