2016-07-27 31 views
0

所以這是一個簡單的文字遊戲im開始工作。 當我運行程序時,它會在它完成之前多次遍歷它們。不是每個人都喜歡我想要的一個。但我仍然希望它隨機選擇它們,所以我不確定是否有更好的方法來做到這一點?有人能告訴我我做錯了什麼嗎?文本遊戲if check1 and check2 and check3 statment

import random 
from collections import defaultdict 

Blist = ["hash browns", "eggs", "cheese", "sausage"] 
Dictionary = {"a":1,"b":1,"c":1,"d":1} 
Tup = (1,2,3,4,5) 
check1 = False 
check2 = False 
check3 = False 

def question(): 
    answer = ((Blist), (Dictionary), (Tup)) 
    new = (random.choice(answer)) 
    print(new) 
    print("which is this?") 
    print("a List, Dictionary, or Tuple?") 
    x = str(input('what is it?\n')) 
    if check1 == True and check2 == True and check3 == True: 
     multiL() 
    else: 
     if x == 'list' and new == Blist: 
      global check1 
      check1 = True 
      print("that is correct, this is a list") 
      question() 
     if x == 'dictionary' and new == Dictionary: 
      global check2 
      check2 = True 
      print("that is correct, this is a dictonary") 
      question() 
     if x == 'tuple' and new == Tup: 
      global check3 
      check3 = True 
      print("that is correct, this is a tuple") 
      question() 
     if x == 're': 
      multiL() 
     else: 
      print("that is not one of the choices, try again?\n") 
      question() 

def multiL(): 
    print("done") 

question() 

回答

0

您可以將函數內的第一行移到函數外部。現在,使其成爲一個列表:

awnser = [(Blist), (Dictionary), (Tup)] 

def question(): 
... 

然後選擇其中的一個隨機函數中:

def question(): 
    newawn = (random.choice(awnser)) 

當一個選擇,現在可以從列表中刪除相應的選擇,以避免重複:

if x == 'list' and newawn == Blist: 
     global check1 
     check1 = True 
     print("that is correct, this is a list") 
     awnser.remove(Blist) 
     question() 

您可以使用awnser.remove()刪除從列表中的特定項目,如在這種情況下。

0

可以移動問題功能外的部分

awnser = ((Blist), (Dictionary), (Tup)) 

然後在那裏,如果你檢查它是否是正確的答案,請從awnser FX的可能性:

 if x == 'list' and newawn == Blist: 
      global check1 
      check1 = True 
      print("that is correct, this is a list") 
      awnser = ((Dictionary), (Tup)) #New line, now with tuple. 
      question() 

當元組是空的,所有3種可能性已經完成。你可以結束。

Try: 
    newawn = (random.choice(awnser)) 
Except IndexError: 
    print("done") 
    return 

P.S.這是拼寫的答案。

+0

AttributeError的:「元組」對象有沒有屬性「刪除」我不斷收到此錯誤 – Doppel

+0

和即時通訊是一個白癡即時得到這個想法 – Doppel

+0

這是我的元組不好,我沒有看過你的代碼正確。對於刪除工作,安納斯必須是一個列表。我已經在帖子中編輯了代碼,成爲一個疙瘩。 –

0

您可以通過將這些類型放入列表中,並在正確識別時刪除每個類型來完成。一段時間的邏輯可以通過使用一個循環來簡化,而不是遞歸地調用它自己。您也不需要全局變量check#,因爲您只能看到類型列表中有任何剩餘部分 - 如果不是,則表示每個變量都已被正確識別。

import random 

Blist = ["hash browns", "eggs", "cheese", "sausage"] 
Dictionary = {"a": 1, "b": 1, "c": 1, "d": 1} 
Tup = (1, 2, 3, 4, 5) 

def question(): 
    choices = [Blist, Dictionary, Tup] 

    while choices: # any left? 
     choice = random.choice(choices) # pick a remaining one 
     print() 
     print(choice) 
     correct_answer = False 
     answer = input('what is this?\na List, Dictionary, or Tuple?\n') 
     answer = answer.lower() 
     if answer == 'list' and choice == Blist: 
      print("that is correct, this is a list") 
      correct_answer = True 
     elif answer == 'dictionary' and choice == Dictionary: 
      print("that is correct, this is a dictonary") 
      correct_answer = True 
     elif answer == 'tuple' and choice == Tup: 
      print("that is correct, this is a tuple") 
      correct_answer = True 
     elif answer == 're': 
      break 
     else: 
      print("that is not one of the choices, try again?\n") 
     if correct_answer: 
      choices.remove(choice) # remove it from list of choices 

    multiL() 

def multiL(): 
    print("done") 

question()