简体   繁体   English

你如何让某物在 python 中循环特定次数?

[英]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.

相关问题 你如何让 python 使用字符串字母的顺序作为字典的一部分,其中每个唯一字母出现的次数的值 - How do you make python use the order of the letters of a string as part of a dictionary with values for the amount of times each unique letter appears 在Python中,如何循环并仅写入特定次数的文件? - In Python, how do you loop and write to a file for only a specific number of times? 在 python 中,你如何按下一个特定的键来做某事? - How do you press a specific key to do something in python? 如何在 python 中每 n 秒重复 x 次? - how to make something repeat x amount of times every n seconds in python? 如何在 Python 3 的特定行中写一些东西? - How do you write something in a specific line in Python 3? 在 Python 中循环 4 次 - Loop something 4 times in Python 如何进行验证循环以确保用户在 a.split 输入中输入正确的次数? - How do I make a validation loop to make sure the user inputs the correct amount of times in a .split input? 如何控制将某些内容添加到列表的次数? - How do I control the amount of times something is added to a list? Python:如何获得一个程序来提示用户一定的固定时间来输入用户信息? - Python: How do you get a programme to prompt the user a certian fixed amount of times for a user input? 你如何让 Python 向用户询问输入“x”的次数? - How do you get Python to ask the user for an input 'x' amount of times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM