saopig 发表于 2020-4-6 15:02:51

新手求助!

import random
score,player=0,['ClimbRoc Present']
def generate():               #定义generate函数,用来随机生成table
      pos_ion=('{'+str(random.randint(0,3))+'}').format('H','K','B','A')
      if pos_ion=='H':
            neg_ion=('{'+str(random.randint(0,2))+'}').format('S','N','C')
      elif pos_ion=='K':
            neg_ion=('{'+str(random.randint(0,2))+'}').format('S','C','O')
      elif pos_ion=='B':
            neg_ion=('{'+str(random.randint(0,2))+'}').format('N','C','O')
      elif pos_ion=='A':
            neg_ion=('{'+str(random.randint(0,1))+'}').format('N','O')
      table=
   #如果在这里输入print(table)是可以正常打印出table的
def prize(time):
    while time:             #随机向player中添加元素
      player.append(('{'+str(random.randint(0,3))+'}').format('H','K','B','A'))
      player.append(('{'+str(random.randint(0,3))+'}').format('S','N','C','O'))
      time-=1
generate()             #唯一一次调用generate
print(table)            #这是错误所在的行(line 33)

---------------------------------
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\ion games.py", line 33, in <module>
    print(table)
NameError: name 'table' is not defined

报错说我没定义table,不知道哪错了...求大家指点


BngThea 发表于 2020-4-6 15:10:25

函数里面最后要返回
写个 returntable

sunrise085 发表于 2020-4-6 15:11:54

你在另一个函数中定义的table,table是那个函数的局部变量,在外部是无法访问的。这是作用域问题。本函数中你没有定义过table,所以程序根本不知道table是什么,于是就会报错了。

saopig 发表于 2020-4-6 15:23:42

sunrise085 发表于 2020-4-6 15:11
你在另一个函数中定义的table,table是那个函数的局部变量,在外部是无法访问的。这是作用域问题。本函数中 ...

那应该怎么办???

saopig 发表于 2020-4-6 15:27:07

BngThea 发表于 2020-4-6 15:10
函数里面最后要返回
写个 returntable

table=
return table
还是不行{:5_99:}

943623021 发表于 2020-4-6 15:32:45

saopig 发表于 2020-4-6 15:27
table=
return table
还是不行

import random
score,player=0,['ClimbRoc Present']
def generate():               #定义generate函数,用来随机生成table
      pos_ion=('{'+str(random.randint(0,3))+'}').format('H','K','B','A')
      if pos_ion=='H':
            neg_ion=('{'+str(random.randint(0,2))+'}').format('S','N','C')
      elif pos_ion=='K':
            neg_ion=('{'+str(random.randint(0,2))+'}').format('S','C','O')
      elif pos_ion=='B':
            neg_ion=('{'+str(random.randint(0,2))+'}').format('N','C','O')
      elif pos_ion=='A':
            neg_ion=('{'+str(random.randint(0,1))+'}').format('N','O')
      table=
      return table
   #如果在这里输入print(table)是可以正常打印出table的
def prize(time):
    while time:             #随机向player中添加元素
      player.append(('{'+str(random.randint(0,3))+'}').format('H','K','B','A'))
      player.append(('{'+str(random.randint(0,3))+'}').format('S','N','C','O'))
      time-=1
result = generate()             #唯一一次调用generate
print(result)            #这是错误所在的行(line 33)
返回置需要用一个变量名来接收

sunrise085 发表于 2020-4-6 15:33:23

saopig 发表于 2020-4-6 15:23
那应该怎么办???

在函数最后写上return table
然后调用函数的时候,将函数返回值赋值给一个变量。
table=generate()
这样就将函数内部变量取出来了,之后就可以print了。

saopig 发表于 2020-4-6 15:46:47

943623021 发表于 2020-4-6 15:32
import random
score,player=0,['ClimbRoc Present']
def generate():               #定义generate ...

十分感谢!!!

saopig 发表于 2020-4-6 15:47:58

sunrise085 发表于 2020-4-6 15:33
在函数最后写上
然后调用函数的时候,将函数返回值赋值给一个变量。



谢谢!涨姿势了!!
页: [1]
查看完整版本: 新手求助!