Crossin的编程教室

标题: 自编python课程小游戏,问题求助!!! [打印本页]

作者: 落梦    时间: 2019-11-24 23:24
标题: 自编python课程小游戏,问题求助!!!





from random import choice
score_you = 0
score_com = 0
print('plaese select your direction !')
print('left front right')
def fof():
    you = input()
    print ('you select' + you)
    l = ['left' 'front' 'right']
    com = choice (l)
    print('computer saved'+ com)
    if you!=com:
        score_you = score_you + 1
        print ('good')
    else:
        score_com = score_com + 1
        print ('bad')
fof()
fof()
fof()
fof()
if score_com < score_you:
    print('congrdulation! you win')
else:
    print('disapproint! you faild')python基础27课小游戏自编代码,错误如下:


"C:\Program Files\Python38\python.exe" C:/Users/23593/Desktop/.idea/list切片.py
plaese select your direction !
left front right
left
Traceback (most recent call last):
  File "C:/Users/23593/Desktop/.idea/list切片.py", line 18, in <module>
you selectleft
computer savedleftfrontright
    fof()
  File "C:/Users/23593/Desktop/.idea/list切片.py", line 13, in fof
    score_you = score_you + 1
UnboundLocalError: local variable 'score_you' referenced before assignment


求解!!!!!




作者: crossin先生    时间: 2019-11-25 00:20
函数里加上
global score_you

去看下 变量的作用域 https://python666.cn/cls/lesson/71/




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5