繁体   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