我需要一种方法来为函数分配随机值,调用函数并将值打印到屏幕上。
当我按原样运行代码时,不会重新计算敌人的攻击和用户的防御。每次调用函数时,我该怎么做才能让 Python 重新计算这些变量?
import random
enemyName = "Crimson Dragon"
def dragonAtk():
return random.randint(5,10)
def userDef():
return random.randrange(8)
userHp = 100
userName = input("What is your name? ")
enemyAttackname = "Fire Blast"
def enemyAttacks():
global battleDmg
global userHp
global enemyAtk
global userDef
enemyAtk = dragonAtk()
userDef = userDef()
print (">>> " + enemyName + " attacks " + userName + " with " + enemyAttackname + "!")
if enemyAtk < userDef:
print (">>> " + userName + " successfully defended the enemy's attack!")
elif enemyAtk == userDef:
print (">>> " + userName + " successfully parried the enemy's attack!")
else:
battleDmg = enemyAtk - userDef
userHp -= battleDmg
print (">>> " + userName + " takes " + str(battleDmg) + " DMG! "\
+ userName + " has " + str(userHp) + " HP remaining!")
enemyAttacks()
input()
enemyAttacks()
input()
这是我的结果
What is your name? Murk
>>> Crimson Dragon attacks Murk with Fire Blast!
>>> Murk takes 6 DMG! Murk has 94 HP remaining!
Traceback (most recent call last):
File "C:\Users\Junior\Desktop\python projects\test", line 37, in <module>
enemyAttacks()
File "C:\Users\Junior\Desktop\python projects\test", line 22, in enemyAttacks
userDef = userDef()
TypeError: 'int' object is not callable
>>>
所以,我看到它跑过一次敌人攻击(),但第二次给了我一个错误。不知道该怎么做。有什么想法吗?