簡體   English   中英

你如何讓某物在 python 中循環特定次數?

[英]how do you make something loop a specific amount of times in python?

你好,我正在嘗試制作一個戰錘 40k 骰子滾輪,我制作了這個代碼,但是它沒有滾動正確數量的骰子

import  random
count = 0

dice_to_roll=input("how many dice are you rolling? ")
hit = input("what do you hit on? ")
print("rolling dice!!!!!!")

while str(count) < dice_to_roll:
    die = random.randint(1, 6)
    count = count + 1
    if str(die) >= hit:
        print(die)
    else:
        print("done") 

input正在返回一個字符串。 使另一個成為字符串並檢查相等性是比較字符串的長度,而不是值。 要使您的工作正常,請使用int(input(...))並刪除您正在執行的str()強制轉換。

代碼:

import random
dice_to_roll = int(input("how many dice are you rolling? "))
hit = int(input("what do you hit on? "))
print ("rolling dice!!!!!!")

for i in range(dice_to_roll):
    die = random.randint(1,6)
    if die>=hit:
        print(f"dice result:{die},hit:{hit},hit success!")
    else:
        print(f"dice result:{die},hit:{hit},hit failed!")
print("done")

結果:

how many dice are you rolling? 1
what do you hit on? 3
rolling dice!!!!!!
dice result:6,hit:3,hit success!
done

不要將您的count轉換為字符串,而是嘗試將您的輸入dice_to_roll轉換為 int。

暫無
暫無

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

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