找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2142|回复: 2

[求助] 初学新人求指导,实在不懂错误出处

1

主题

1

帖子

1

积分

贫民

积分
1
soy 发表于 2017-9-22 17:34:09 | 显示全部楼层 |阅读模式
#-*- coding: UTF-8 -*-

import random

def Game(self, palyer, all_poker):
     all_pokers = all_poker
     all_pokers = ['big_king', 'small_king', 'flower_2', 'flower_3', 'flower_4', 'flower_5', 'flower_6', 'flower_7',
                 'flower_8', 'flower_9', 'flower_10', 'flower_J', 'flower_Q', 'flower_K', 'flower_A', 'square_2',
                 'square_3', 'square_4', 'square_5', 'square_6', 'square_7', 'square_8', 'square_9', 'square_10',
                 'square_J', 'square_Q', 'square_K', 'square_A', 'black_2', 'black_3', 'black_4', 'black_5', 'black_6',
                 'black_7', 'black_8', 'black_9', 'black_10', 'black_J', 'black_Q', 'black_K', 'black_A', 'red_2',
                 'red_3', 'red_4', 'red_5', 'red_6', 'red_7', 'red_8', 'red_9', 'red_10', 'red_J', 'red_Q', 'red_K',
                 'red_A']

     player = random.sample(all_pokers,17)
     usera = dict[player]
     if len(usera) > 0:
         all_pokers = all_pokers.remove(usera)
         userb = random.sample(all_pokers,17)
         if len(userb) > 0:
             all_pokers = all_pokers.remove(userb)
             userc = random.sample(all_pokers,17)
             if len(userc) > 0:
                 all_pokers = all_pokers.remove(userc)
                 userdizhu = all_pokers
     else:
          print ERROR

print '玩家A的牌是:'+ usera
print '玩家B的牌是:'+ userb
print '玩家C的牌是:'+ userc
print '地主抢牌:'+ userdizhu


报错:  
File "E:/tool/pycharm/PyCharm Community Edition 2017.2/works/doudizhugame.py", line 29, in <module>
    print '玩家A的牌是:'+ usera
NameError: name 'usera' is not defined

感谢!!!!
回复

使用道具 举报

0

主题

16

帖子

16

积分

贫民

积分
16
左手十字 发表于 2017-9-23 01:32:26 来自手机 | 显示全部楼层
因为你定义的usera,userb,userc,userdizhu都是局部变量,它们的作用域只在函数内,你在全局作用域是无法使用的
回复 支持 反对

使用道具 举报

0

主题

7

帖子

7

积分

贫民

积分
7
snakeqx 发表于 2017-9-23 21:20:32 | 显示全部楼层
前面定义了一个Game的函数,usera和其他的变量都是函数内部的。
后面print的时候没有缩进,已经在函数范围外了。所以认为usera没有被定义
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表