猿问

函数中的积分变量不起作用

我正在尝试使第一个函数 opt1() 使健康下降 50 点,但是由于某种原因它不起作用。该代码在不在函数中时有效。不起作用的行: health = int(health) - 50


import random



health = "100"

p = "1"



def opt1():

   print("Du förlorade striden")

   health = int(health) - 50

   print(health)



def opt2():

   print("hej2")



def opt3():

   print("hej3")



q = [opt1, opt2, opt3]

ind = random.randint(0,len(q)-1)



print("you have ", p, " potions")

print("Your health is ", health,)

while True:

   print("Commands: Add, drink, sleep, fight, quest")

   a = input("Enter a command: ")

   if a == "add":

       health = int(health)

       p = int(p) + 1

       print("you have ", p, " potions")

       print("Your health is ", health,)

   if a == "fight":

       q[ind]()


开满天机
浏览 157回答 2
2回答

猛跑小猪

def opt1():在 opt1 中,您应该声明您提供给函数的变量。例如:def opt1(health):
随时随地看视频慕课网APP

相关分类

Python
我要回答