2013-04-07 48 views
1

我試圖重擲多個骰子,並且每次都要記住之前的重擲。所以,舉個例子,如果我擲出5死,得到1,2,3,4,5。我問你要重擲哪個死亡-1,3,4,然後得到類似3,2,5,3,5的東西。但正如我在循環中所要求的那樣,它會覆蓋以前的新卷並只給出最後一卷。當我在循環中運行時,如何存儲新發現的號碼?試圖在循環中存儲信息

reroll1 = input("Would you like to reroll? Yes or No: ") 
if reroll1 == "Yes" or "yes": 
count = 0 
times = int(input("How many die would you like to reroll? ")) 
while count < times: 
    whichreroll = input("Reroll die: ") 
    if whichreroll == "1": 
     reroll1 = random.randint(1,6) 
    else: 
     reroll1 = die1 
    if whichreroll == "2": 
     reroll2 = random.randint(1,6) 
    else: 
     reroll2 = die2 
    if whichreroll == "3": 
     reroll3 = random.randint(1,6) 
    else: 
     reroll3 = die3 
    if whichreroll == "4": 
     reroll4 = random.randint(1,6) 
    else: 
     reroll4 = die4 
    if whichreroll == "5": 
     reroll5 = random.randint(1,6) 
    else: 
     reroll5 = die5 
    newset = [reroll1, reroll2, reroll3,reroll4,reroll5] 

    count += 1 
    print(newset) 
+1

這是什麼語言?蟒蛇?僞?請添加標籤和/或編寫它的語言。 – feralin 2013-04-07 22:07:10

+2

僅供參考:'如果reroll1 ==「是」或「是」:'不按照您的想法做。你應該嘗試'如果reroll1.lower()==「是」:'而不是。 – DaoWen 2013-04-07 22:11:25

+1

你的問題不是很清楚。所以如果你輸入4,你想要最後一次reroll4掛?或者如果你輸入4,你想要reroll1,2,3,5保持他們以前的卷? – 2013-04-07 22:14:01

回答

0

你可以只遍歷一個骰子塊,而不必所有的if語句乾淨的東西了不少。這是通過使用選定的骰子來索引現有的骰子列表。我假設你已經有了原始擲骰子列表,所以我只做了一個並複製到了新的骰子上。

oldset = [1,2,3,4,5] 
reroll1 = str(raw_input("Would you like to reroll? Yes or No: ")) 
if reroll1.lower() == "yes": 
    count = 0 
    times = int(input("How many die would you like to reroll? ")) 
    newset = oldset 

    while count < times: 
     whichreroll = input("Reroll die: ") 
     reroll = random.randint(1,6) 
     newset[whichreroll-1]= reroll 
     count += 1 
     print(newset) 
0

如果我得到了你的問題所在,你可以簡單地做到這一點有兩個集列表,一個是你已經擁有這是newset,另一種是prevSet之一。 prevSet存儲結果的最後一個值,所以基本上你可以在每次迭代開始時初始化prevSet,所以它會是

while (count < times): 
    prevSet = newset 
    . 
    . 
    . 
+0

我基本上做了同樣的事情,除了設置我的原始列表(這是dieset,這裏沒有列出),代替newset,本質上(我認爲),在每次迭代後恢復所有內容。之前它只會改變循環設置方式中最後一個死亡數字,因此它不會存儲先前發現的新數字,然後在最後一次運行時只更改最新的數字。無論如何,它現在正在工作:)感謝您的迴應! – Jen 2013-04-08 04:45:25