此代碼通過正常運行一次,然後帶來的誤差斷碼,需要幫助固定
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
paper
NameError: name 'paper' is not defined
我需要的代碼能夠運行,石頭,蜥蜴,斯波克運行10次的遊戲顯示10分(或選定的數量)比賽結束時的得分(因爲它是和)。 下面是代碼:
import random
numberofgames = raw_input("How many games do you want to play? ")
print "Please choose rock , paper , scissors , lizard, or spock (in lower case please)"
choice = raw_input("What do you choose? ")
player_choice = str(choice)
def name_to_number(name):
if name == "rock":
name = 0
return name
elif name == "spock":
name = 1
return name
elif name == "paper":
name = 2
return name
elif name == "lizard":
name = 3
return name
elif name == "scissors":
name = 4
return name
def number_to_name(number):
if number == 0:
number = "rock"
return number
elif number == 1:
number = "spock"
return number
elif number == 2:
number = "paper"
return number
elif number == 3:
number = "lizard"
return number
elif number == 4:
number = "scissors"
return number
try:
computer_choice = random.randrange(5)
player_number = name_to_number(player_choice)
print "Player choice is: " + player_choice
print "Computer choice is: " + number_to_name(computer_choice)
difference = (int(player_number) - computer_choice) % 5
draws = 0
playerwins = 0
computerwins = 0
if difference in [1, 2]:
print "Player wins!"
playerwins = playerwins + 1
elif difference == 0:
print "Player and computer tie!"
draws = draws + 1
else:
print "Computer wins!"
computerwins = computerwins + 1
print "Wins: " + str(playerwins) + "\n" + "Draws: " + str(draws) + "\n" + "Losses " + str(computerwins)
while playerwins + draws + computerwins <= numberofgames:
name_to_number()
except TypeError:
print "Sorry, please read the directions and type rock, paper, scissors, spock, or lizard in lowercase."
這是你的整個代碼?這個錯誤似乎不是來自這段代碼。 – DeepSpace
您昨天問過這個,並將其刪除了 – muddyfish
歡迎來到Stack Overflow!這個錯誤意味着你在使用'paper'作爲變量之前告訴Python它應該保持什麼價值,但我沒有看到你在這個腳本中這樣做。你在這個腳本的同一目錄下是否有一個名爲'random.py'的文件? – SuperBiasedMan