私はこのゲームで動かそうとすると、変数に対してUnboundLocalErrorを返します。私はそれが起こっている理由を理解していない、誰かが私にそれを説明し、それを修正する方法を教えてくれる?テキストベースのゲームでのUnboundLocalError
pro = '> '
import random
chamber = random.randint(1, 6)
def initialMenu():
print "Welcome to Russian Roulette!"
print 'Type "R" for the rules or "P" to play.'
play_or_rules = raw_input(pro)
if play_or_rules == 'r' or play_or_rules == 'R':
rules()
elif play_or_rules == "P" or play_or_rules == 'p':
turn()
else:
print 'ERROR: Must type in "R" or "P" at the beginning. Restarting program.\n'
initialMenu()
def rules():
print "You have a revolver. You and 1 friend play this game. You load 1 bullet into one of the six chambers."
print "On your turn, you can pull the trigger on yourself, on your opponent and then yourself, or spin the chamber."
print "You may spin the chamber as much as you like before firing."
print "Once you fire at yourself, your turn ends, and your friend's begins."
print "Keep in mind that the game isn't completely random; there can be some strategy to it.\n"
initialMenu()
def turn():
print 'Type "Q" to shoot yourself, "W" to shoot your opponent and then yourself, or "E" to spin the chamber.'
move = raw_input(pro)
if move == 'q' or move == 'Q':
if chamber == 1:
print "BANG! Your brain splatters on the wall. YOU LOSE"
else:
print "CLICK! You live another round."
chamber += 1
if chamber == 7:
chamber = 1
turn()
else:
turn()
elif move == 'W' or move == "w":
if chamber == 6:
print "CLICK! Your opponent lives."
print "BANG! You don't."
print "YOU LOSE"
#first shot would be blank, second wouldn't
elif chamber == 1:
print "BANG! Your opponent's head has a hole in it."
print "YOU WIN"
else:
print "CLICK! Your opponent lives."
print "CLICK So do you."
chamber += 2
if chamber == 7:
chamber = 1
turn()
elif chamber == 8:
chamber = 2
turn()
else:
turn()
elif move == 'e' or move == 'E':
chamber = random.randint(1, 6)
print "You spun the chamber, but it is still your turn."
turn()
initialMenu()
ありがとうございます!
Traceback (most recent call last):
File "C:/Python27/russianroulette.py", line 62, in <module>
initialMenu()
File "C:/Python27/russianroulette.py", line 11, in initialMenu
turn()
File "C:/Python27/russianroulette.py", line 61, in turn
turn()
File "C:/Python27/russianroulette.py", line 27, in turn
if chamber == 1:
UnboundLocalError: local variable 'chamber' referenced before assignment
*フル*エラー(スタックトレース付き)を投稿してください。こうすれば、人々はどこから見えるかを知ることができます。つまり、UnboundLocalErrorは、存在しない変数を使用しようとしていることを意味します。タイプミスがないか確認してください。 – Carpetsmoker
完全なスタックトレースを投稿してください。 –