[英]how do you make something loop a specific amount of times in python?
hello I am trying to make a warhammer 40k dice roller and I made this code however it does not roll the right amount of dice I would appreceate help on how to make it work你好,我正在尝试制作一个战锤 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
is returning a string. input
正在返回一个字符串。 Making the other one a string and checking equality is comparing the lengths of the string, not the values.使另一个成为字符串并检查相等性是比较字符串的长度,而不是值。 To make yours work, use int(input(...))
and remove the str()
casts that you're doing.要使您的工作正常,请使用int(input(...))
并删除您正在执行的str()
强制转换。
Code:代码:
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")
Result:结果:
how many dice are you rolling? 1
what do you hit on? 3
rolling dice!!!!!!
dice result:6,hit:3,hit success!
done
Instead of converting your count
to a string try and convert your input dice_to_roll
to an int.不要将您的count
转换为字符串,而是尝试将您的输入dice_to_roll
转换为 int。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.