如何在while循环中每次获得不同的随机结果?

2024-09-27 23:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

import random
randatk=random.randint(1,10)  
randheal=random.randint(1,10)
mnstrhp=100
playerhp=100
while mnstrhp>0:
    atkorheal=input("would you like to attack the monster or heal yourself? ")
    if 'attack' in atkorheal:
        print("you did" , randatk , "damage to the monster!")
        print("the monster's current health is " , mnstrhp-randatk)
    if 'heal' in atkorheal:
        print("you healed for" , randheal  , "health!")
        print("your current hp is " , playerhp+randheal)

我的目标是每次程序循环时得到不同的随机结果,但是每次程序循环时,它总是相同的数字。有没有办法每次都得到不同的值


Tags: thetoyouifrandomprintrandintattack

热门问题