Python2.7:这个程序有什么问题?它运行,但奇怪的回溯错误
程序运行良好。但是用这个截图。它只是一片空白。它的Python 2.7。我还将Python添加到了环境变量中,但shell上也没有显示任何内容 石头布和剪刀代码 错误:Python2.7:这个程序有什么问题?它运行,但奇怪的回溯错误,python,python-2.7,Python,Python 2.7,程序运行良好。但是用这个截图。它只是一片空白。它的Python 2.7。我还将Python添加到了环境变量中,但shell上也没有显示任何内容 石头布和剪刀代码 错误: 回溯最近的调用上次:文件 C:/Users/Sarthak/Desktop/RPS.py,第80行,在 如果名称='main':名称错误:未定义名称'name' 您应该修复缩进和所有其他错误: import random import time rock = 1 paper = 2 scissors = 3 names =
回溯最近的调用上次:文件 C:/Users/Sarthak/Desktop/RPS.py,第80行,在 如果名称='main':名称错误:未定义名称'name'
您应该修复缩进和所有其他错误:
import random
import time
rock = 1
paper = 2
scissors = 3
names = { rock: "Rock" , paper: "Paper" , scissors: "Scissors" }
rules = { rock: scissors , paper :rock , scissors: paper }
player_score = 0
computer_score = 0
def start():
print "Let's play a game of rock paper and scissors"
while game():
pass
scores()
def game():
player = move()
computer = random.randint(1,3)
result(player, computer)
return play_again()
def move():
while True:
print #this is not how you could get int input in Python
player = raw_int("Rock = 1\nPaper = 2\nScissors =3\nMake a move: ")
try:
player = int(player)
if player in (1,2,3):
return player
except ValueError:
pass
print "Oops! I didn't understand that. Please enter 1,2 or 3." #note the indentation here
def result(player, computer):
print "1..."
time.sleep(1)
print "2..."
time.sleep(1)
print "3..."
time.sleep(0.5)
print "Computer threw {0)!".format(names[computer])
global player_score,computer_score
if player == computer:
print "Tie game."
else:
if rules[player] == computer:
print "Your victory has been assured."
player_score += 1
else:
print" The computer laughs as you realise you have been defeated."
computer_score += 1
def play_again(): #again, indentation
#and this is not how you could get string input in Python
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("Y", "Y" , "yes" , "Yes" , "Of course!"):
return answer
else:
print "Thank you very much for playing our game.See your next time!"
def scores(): #note the indentation here
global player_score,computer_score
print "High Scores"
print "Player:" , player_score
print "Computer:", computer_score
if __name__ == '__main__': #note the underscores here
start()
我是Python新手。如何呼叫start?我确实打过电话。但是还有一个错误,uni-dented与外部缩进级别不匹配,正如回答者所示,您可以将start粘贴在程序的底部。@BHouwens我确实将它粘贴在了底部。但是,还有一个错误,Unidented与外部标识级别不匹配。@SarthakBhatia在程序的底部调用它,并且在底部不提供任何缩进all@BHouwens现在有一个最近的回溯调用last:File C:/Users/Sarthak/Desktop/RPS.py,第80行,在if name='main':name错误:name'name'未定义您的if\u name\u='\uu main\uuu'未正确缩进,现在它在您的分数范围内function@MaxNoe谢谢,现在它运行了,但有一个回溯错误。在上面添加了屏幕截图。回溯最近的通话最后:文件C:/Users/Sarthak/Desktop/RPS.py,第80行,在if name='main':name错误:name'name'未定义请建议一个…-这个问题与您的主要问题完全无关。通常,我会告诉您为每个新主题创建一个新问题,但这种问题在“禁止主题4”页面中被明确列为非主题,所以请删除它。将准确地告诉您要去哪里。请不要编辑您的问题以包含新问题。相反,创建一个新问题。我还建议您在提出下一个问题之前阅读,因为您似乎还没有掌握有关堆栈溢出问题的一些规则。
import random
import time
rock = 1
paper = 2
scissors = 3
names = { rock: "Rock" , paper: "Paper" , scissors: "Scissors" }
rules = { rock: scissors , paper :rock , scissors: paper }
player_score = 0
computer_score = 0
def start():
print "Let's play a game of rock paper and scissors"
while game():
pass
scores()
def game():
player = move()
computer = random.randint(1,3)
result(player, computer)
return play_again()
def move():
while True:
print #this is not how you could get int input in Python
player = raw_int("Rock = 1\nPaper = 2\nScissors =3\nMake a move: ")
try:
player = int(player)
if player in (1,2,3):
return player
except ValueError:
pass
print "Oops! I didn't understand that. Please enter 1,2 or 3." #note the indentation here
def result(player, computer):
print "1..."
time.sleep(1)
print "2..."
time.sleep(1)
print "3..."
time.sleep(0.5)
print "Computer threw {0)!".format(names[computer])
global player_score,computer_score
if player == computer:
print "Tie game."
else:
if rules[player] == computer:
print "Your victory has been assured."
player_score += 1
else:
print" The computer laughs as you realise you have been defeated."
computer_score += 1
def play_again(): #again, indentation
#and this is not how you could get string input in Python
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("Y", "Y" , "yes" , "Yes" , "Of course!"):
return answer
else:
print "Thank you very much for playing our game.See your next time!"
def scores(): #note the indentation here
global player_score,computer_score
print "High Scores"
print "Player:" , player_score
print "Computer:", computer_score
if __name__ == '__main__': #note the underscores here
start()