新人求助!Python 代码报错“name not defined”

云小楼 2018-06-08 08:44:12
def rpsls(player_choice):
# print out the message for the player's choice
print 'the player chooses ' + player_choice
# convert the player's choice to player_number using the function name_to_number()
return name_to_number(player_choice)
# compute random guess for comp_number using random.randrangedef comp_number(c_number):def comp_number(number):
def comp_number():
return random.randrange(0,5)

# convert comp_number to comp_choice using the function number_to_name()
def comp_choice():

print 'the computer chooses '+ number_to_name(comp_number())

# compute difference of comp_number and player_number modulo five
if comp_number()-rpsls(player_choice) == 1:
print 'Computer wins!'
elif comp_number()-rpsls(player_choice) == 2:
print 'computer wins'

报错的是标红的这一栏, player_choice没有定义,但是我前面是def rpsls(player_choice)的呀,为什么还是显示没定义呢?谢谢各位
...全文
1565 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
雷中听风 2018-06-10
  • 打赏
  • 举报
回复
def rpsls(player_choice):    
    # print out the message for the player's choice
    print 'the player chooses ' + player_choice
    # convert the player's choice to player_number using the function name_to_number()
    return name_to_number(player_choice)
    # compute random guess for comp_number using random.randrangedef comp_number(c_number):def comp_number(number):  
def comp_number():
        return random.randrange(0,5)
       
    # convert comp_number to comp_choice using the function number_to_name()
def comp_choice():
    
    print 'the computer chooses '+ number_to_name(comp_number())
      
    # compute difference of comp_number and player_number modulo five
    if   comp_number()-rpsls(player_choice) == 1: 
        print 'Computer wins!'
    elif comp_number()-rpsls(player_choice) == 2:
        print 'computer wins'
这就清晰多了,显然没有定义,记住变量的作用域。
陈年椰子 2018-06-08
  • 打赏
  • 举报
回复

 if   comp_number()-rpsls(player_choice) == 1: 
语句前, 你要给 player_choice 赋值, 通过input 或者 直接 player_choice = 1 之类的。 还有 name_to_number 这个函数也没定义。

player_choice = 1
if   comp_number()-rpsls(player_choice) == 1: 
裸睡的雨 2018-06-08
  • 打赏
  • 举报
回复
你这代码让人看起来好乱,就不能截图吗还有你下面是调用那个方法,不应该是传值吗

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧