Python 重复“If”语句块
我正在尝试制作一个基于文本的游戏,为了制作我的战斗机制,我必须重复一个if语句,直到你或怪物死亡 给你- Ph代表玩家生命值,str代表玩家力量值,mdam代表怪物伤害值,mstr代表怪物力量值,怪物H代表怪物健康值,随机数。随机数是你获得的攻击加成,有没有办法将数字四舍五入到小数点后一位或零位?谢谢DPython 重复“If”语句块,python,Python,我正在尝试制作一个基于文本的游戏,为了制作我的战斗机制,我必须重复一个if语句,直到你或怪物死亡 给你- Ph代表玩家生命值,str代表玩家力量值,mdam代表怪物伤害值,mstr代表怪物力量值,怪物H代表怪物健康值,随机数。随机数是你获得的攻击加成,有没有办法将数字四舍五入到小数点后一位或零位?谢谢D if fight == "A" and ph > 0: damage = (str) + (random.random()) monsterh = 6 mstr =
if fight == "A" and ph > 0:
damage = (str) + (random.random())
monsterh = 6
mstr = 0.9
monsterh = (monsterh) - (damage)
print(damage)
print(damage - str)
print('You attack the monster. You deal %s damage.' % damage )
time.sleep(3)
if monsterh > 0:
mstr = 0.9
mdam = (mstr) + (random.random())
print('The monster attacks you!')
ph = ph - mstr
print("You get hit %s" % mdam )
else:
xpgain = damage + 5
print("You won! You've gained %s XP!" % xpgain)
将从中获得的值四舍五入
random.random()
你能行
round(random.random(),1)
要四舍五入到小数点后1位,或者如果要去掉小数,请替换
random.random()
与
至于重复if语句,您可以将其放入一个循环中,当满足某个条件时,该循环将中断
while true:
if fight == "A" and ph > 0:
damage = (str) + (int(random.random()))
monsterh = 6
mstr = 0.9
monsterh = (monsterh) - (damage)
print(damage)
print(damage - str)
print('You attack the monster. You deal %s damage.' % damage )
time.sleep(3)
elif ph<=0 or fight!="A":
print("You lose!")
break
if monsterh > 0:
mstr = 0.9
mdam = (mstr) + (int(random.random()))
print('The monster attacks you!')
ph = ph - mstr
print("You get hit %s" % mdam )
else:
xpgain = damage + 5
print("You won! You've gained %s XP!" % xpgain)
break
因为我认为你根本不会改变战斗的价值
你也不必这么做
mdam = (mstr) + (int(random.random()))
你可以这么做
mdam = mstr+int(random.random())
上述内容同样适用于代码中的其他地方。如果您有任何问题,请随时提问。我将把您的战斗总结成一个循环。例如:
while (ph > 0 or monsterh > 0)
# combat rules
# Check who died and move on from there
if (ph > 0)
# next thing the player does
else
# print "You died"
关于舍入,有多种选择,具体取决于您想要做什么:您想要一个while循环。看看。你可以把整个事情放在一个循环中:当我还没死,怪物还没死的时候:。。。
mdam = mstr+int(random.random())
while (ph > 0 or monsterh > 0)
# combat rules
# Check who died and move on from there
if (ph > 0)
# next thing the player does
else
# print "You died"