Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从一个数字中连续减法_Python - Fatal编程技术网

Python 如何从一个数字中连续减法

Python 如何从一个数字中连续减法,python,Python,初学者python用户在这里。我正在为rpg中的基本攻击编写一些代码,看起来是这样的: 随机输入 职业攻击: 健康=300 对于范围1中的i: 如果random.uniform0,10使用while循环 while health > 0: if (random.uniform(0, 10)) <= int(8.2): health = health - 50 print("Your attack hit.\n" + &quo

初学者python用户在这里。我正在为rpg中的基本攻击编写一些代码,看起来是这样的:

随机输入 职业攻击: 健康=300 对于范围1中的i: 如果random.uniform0,10使用while循环

while health > 0:
    if (random.uniform(0, 10)) <= int(8.2):
        health = health - 50
        print("Your attack hit.\n" + "The monster's health is at " + str(health))
    else:
         print("Your attack missed.\n" + "The monster's health is at " + str(health))
您可以使用while循环-在本例中,它将运行直到满足条件,直到运行状况为0:

while health > 0:
    if (random.uniform(0, 10)) <= int(8.2):
            health = health - 50
            print("Your attack hit.\n" + "The monster's health is at " + str(health))
        else:
            print("Your attack missed.\n" + "The monster's health is at " + str(health))

您的循环只执行一次

for i in range(1):
如果希望它一直运行到运行状况为0,则需要一个while循环:


最好使用函数,而不是类,并且可以使用while循环继续攻击小于0的运行状况

import random
def attack():
    health = 300
    while health > 0:
        if (random.uniform(0, 10)) <= int(8.2):
             health -= 50
             print("Your attack hit.\n" + "The monster's health is at " + str(health))
        else:
             print("Your attack missed.\n" + "The monster's health is at " + str(health))
attack()

若要继续,您必须使用比range1更大的值-即range5,命中5次-或者,如果为True,则使用break退出无限循环。或者当生命值>0时:而不是循环。或者当生命值>0时:继续攻击直到你获胜。生命值应该是怪物对象的属性。换句话说,你应该在你的monster类中暗示这一点。你的代码将在没有类攻击的情况下同等工作:line。您可能希望从示例代码中删除,以避免混淆。固定程序的预期输出是什么?
import random
def attack():
    health = 300
    while health > 0:
        if (random.uniform(0, 10)) <= int(8.2):
             health -= 50
             print("Your attack hit.\n" + "The monster's health is at " + str(health))
        else:
             print("Your attack missed.\n" + "The monster's health is at " + str(health))
attack()