簡體   English   中英

為什么添加 function 會中斷我的 while 循環?

[英]Why does the add function interrupt my while loop?

我在 function all_dice() 上工作,它應該取一個選擇值,即用戶想要擲多少個骰子,然后擲出相應數量的骰子。 我想將每個 roll_() 變量存儲在一個空集 thisset=set() 中,以便能夠通過返回和打印 all_dice() 來獲得我的最終分數。 但我得到的只是一個值。 set-idea 似乎攔截了 while 循環。 Fe 當我設置選項 == 5 時,這個集合只包含 {4}。 如果它按預期工作,function 應該從choice=5 開始,獲取一個roll_() 編號,將其存儲在這個set{} 中,然后通過choice=choice-1 倒計時,go 到choice==4,依此類推. 因此,在選擇= 5的情況下,ID期望在這一組中{}中的5個值。

如果沒有 thisset.add() function,all_dice() function 按預期工作 - 倒計時選擇並產生相應的值。

有人可以告訴我為什么會攔截while循環/問題是什么?

這是我的第一個程序,所以請對我溫柔-謝謝您的幫助!

#function to role many dice 
import random
choice =5
print("choice is:",choice)
def all_dice(choice):
    while choice>=1:
        thisset=set()
        if choice==1:
            roll_1=random.randint(1,6)
            thisset.add(roll_1)
            choice=choice-1
        if choice==2:
            roll_2=random.randint(1,6)
            thisset.add(roll_2)
            choice=choice-1
        if choice==3:
            roll_3=random.randint(1,6)
            thisset.add(roll_3)
            choice=choice-1
        if choice==4:
            roll_4=random.randint(1,6)
            thisset.add(roll_4)
            choice=choice-1
        if choice==5:
            roll_5=random.randint(1,6)
            thisset.add(roll_5)
            choice=choice-1
    return thisset
print(all_dice(choice))

每次循環都在清除集合,因此您丟失了以前的項目。 您只需要在循環之前對其進行一次初始化。 (由於某種原因,這種類型的錯誤在初學者中很常見。)

而且您不需要所有這些不同的變量或if語句,因為它們都做同樣的事情。

def all_dice(choice):
    thisset = set()
    for _ in range(choice):
        thisset.add(random.randint(1, 6))
    return thisset

我不確定你為什么要使用一組來擲多個骰子。 一個集合不能有重復,因此如果您多次滾動相同的數字,該集合將只包含其中一個。 如果你想擲 5 個骰子,你通常會使用一個列表來保存所有的擲骰子。 如果您只關心總和,則根本不需要保存所有不同的卷,只需將它們添加到總變量中即可。

這是我能想到的最pythonic的方式。 我不建議使用set()除非你不想在你的數據中重復:

import random

def roll_dice(rolls=1):
    return [random.randint(1, 6) for roll in range(rolls)]

roll_dice(5)

或者另一種方法:

import random

roll_dice = lambda rolls: [random.randint(1, 6) for roll in range(rolls)]

roll_dice(5)

Output:

[2, 1, 1, 5, 1]
#function to role many dice 
import random
choice =5
print("choice is:",choice)
def all_dice(choice):
    thisset = [] 
    while choice>=1:
        if choice==1:
            roll_1=random.randint(1,6)
            thisset.append(roll_1)
            choice=choice-1
        if choice==2:
            roll_2=random.randint(1,6)
            thisset.append(roll_2)
            choice=choice-1
        if choice==3:
            roll_3=random.randint(1,6)
            thisset.append(roll_3)
            choice=choice-1
        if choice==4:
            roll_4=random.randint(1,6)
            thisset.append(roll_4)
            choice=choice-1
        if choice==5:
            roll_5=random.randint(1,6)
            thisset.append(roll_5)
            choice=choice-1
    return thisset
print(all_dice(choice))

我得到的樣品 output 是:-

[3, 1, 6, 5, 2]

set不能有重復項,因此我使用了list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM