我收到此错误消息:
来自This Website的Traceback (most recent call last): File "python", line 256, in <module> File "python", line 11, in questions File "python", line 190, in instructions File "python", line 27, in names File "python", line 45, in p1turn_part_1 File "python", line 137, in p2turn File "python", line 86, in p1turn_part_2 UnboundLocalError: local variable 'p1point' referenced before assignment
。 代码是:
import time
import random
###########
#Functions#
###########
def questions():
instruction_or_skip = input('Type \'instructions\' to learn how to play. If you don\' want to learn, type \'skip\'')
if instruction_or_skip == 'instructions':
instructions()
elif instruction_or_skip == 'skip':
names()
else:
print('\nSorry, I didn\'t get that.\n')
questions()
#--starting screen--#
def startScreen():
loadingScreen()
def names():
global p1name
p1name = input('What is the first player\'s name?')
global p2name
p2name = input('What is the second player\'s name?')
p1turn_part_1()
def p1turn_part_1():
global rounds
print('\nHow many rounds?')
rounds = input(':>')
rounds = int(rounds)
global p1point
p1point = 0
p1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']
p1 = (random.choice(p1))
print p1
if p1 == '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9':
p1point = p1point + 1
time.sleep(1)
global rounds
rounds = rounds - 1
print(p1name + ' gets a point!')
p2turn()
if rounds < 0:
print p1point
print p2point
if p1point < p2point:
print('\nPlayer 2 wins!')
exit(0)
elif p2point < p1point:
print('\nPlayer 1 wins')
exit(0)
else:
print('It\'s a tie!')
exit(0)
else:
time.sleep(1)
print(p1name + ' gets no points...')
p2turn()
global rounds
rounds = rounds - 1
if rounds < 0:
print p1point
print p2point
if p1point < p2point:
print('\nPlayer 2 wins!')
exit(0)
elif p2point < p1point:
print('\nPlayer 1 wins')
exit(0)
else:
print('It\'s a tie!')
exit(0)
else:
p2turn()
def p1turn_part_2():
#1-9 are divisble by 4
#10-27 are divisble by 2
p1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']
p1 = (random.choice(p1))
print p1
if p1 == '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9':
p1point = p1point + 1
time.sleep(1)
global rounds
rounds = rounds - 1
print(p1name + ' gets a point!')
p2turn()
if rounds < 0:
print p1point
print p2point
if p1point < p2point:
print('\nPlayer 2 wins!')
exit(0)
elif p2point < p1point:
print('\nPlayer 1 wins')
exit(0)
else:
print('It\'s a tie!')
exit(0)
else:
time.sleep(1)
print(p1name + ' gets no points...')
global rounds
rounds = rounds - 1
p2turn()
if rounds < 0:
print p1point
print p2point
if p1point < p2point:
print('\nPlayer 2 wins!')
exit(0)
elif p2point < p1point:
print('\nPlayer 1 wins')
exit(0)
else:
print('It\'s a tie!')
exit(0)
else:
p2turn()
def p2turn():
global p2point
p2point = 0
p2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']
p2 = (random.choice(p2))
print p2
if p2 == '10' or '11' or '12' or '13' or '14' or '15' or '16' or '17' or '18' or '19' or '20' or '21' or '22' or '23' or '24' or '25' or '26' or '27':
global p2point
p2point = p2point + 1
time.sleep(1)
global rounds
rounds = rounds - 1
print(p2name + ' gets a point!')
p1turn_part_2()
if rounds < 0:
print p1point
print p2point
if p1point < p2point:
print('\nPlayer 2 wins!')
exit(0)
elif p2point < p1point:
print('\nPlayer 1 wins')
exit(0)
else:
print('It\'s a tie!')
exit(0)
else:
time.sleep(1)
global rounds
rounds = rounds - 1
print(p2name + ' gets no points...')
p1turn_part_2()
if rounds < 0:
print p1point
print p2point
if p1point < p2point:
print('\nPlayer 2 wins!')
exit(0)
elif p2point < p1point:
print('\nPlayer 1 wins')
exit(0)
else:
print('It\'s a tie!')
exit(0)
else:
p1turn_part_2()
#--the instructions--#
def instructions():
print(''' ##########################
#########DIE ROLL#########
##########################''')
print('\nRules:')
print('\n1.)Follow all prompts on screen')
print('2.)Do not modify the script under ANY curcumstances')
print('3.)You do not need to write down any scores or round numbers,')
print(' They will be recorded for you.')
print('4.)P1 needs to get any number divisable by 4')
print('5.)P2 needs to get any number divisable by 2')
print('6.)P3 needs to get any number divisable by 3')
print('7.)P4 needs to get any number divisable by 5')
names()
#--for looks only--#
def loadingScreen():
print('\n' * 100)
print('\nLoading...')
print('\n')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading.')
print('\n-')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading..')
print('\n-')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading...')
print('\n--')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading.')
print('\n--')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading..')
print('\n---')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading...')
print('\n---')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading.')
print('\n----')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading..')
print('\n----')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading...')
print('\n-----')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading.')
print('\n-----')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading..')
print('\n------')
time.sleep(0.5)
print('\n' * 100)
print('\nLoading...')
print('\n------')
time.sleep(0.5)
############
####GAME####
############
print('test complete')
startScreen()
questions()
global rounds
print('\nHow many rounds?')
rounds = input(':>')
rounds = int(rounds)
有谁知道问题是什么? (这也适用于p2point和rounds变量。)(在以前的站点上测试。)
附带问题:
答案 0 :(得分:0)
p1turn_part_2
包含以下声明:
p1point = p1point + 1
要设置p1point
的新值,需要使用旧值。但是之前从未设置变量,因此在尝试将旧值添加1时会出错。
我认为这应该使用p1turn_part_1
中设置的值。该功能有:
global p1point
所以它设置了全局变量。但由于p1turn_part_2
没有这样的语句,它会尝试使用局部变量,而不是全局变量。将相同的global
语句添加到p1turn_part_2
。