|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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=[pos_ion,neg_ion]
#如果在这里输入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,不知道哪错了...求大家指点
在函数最后写上
然后调用函数的时候,将函数返回值赋值给一个变量。
这样就将函数内部变量取出来了,之后就可以print了。
|
|