Python (空白)未在我的石头剪纸程序中定义
我不知道为什么,但这让我很恼火 我的代码:Python (空白)未在我的石头剪纸程序中定义,python,Python,我不知道为什么,但这让我很恼火 我的代码: import random game() def game (): p_choice = raw_input("What do you choose?") cpu_random = random.randint(1,3) cpu_choice = cpu_random if cpu_random == 1: cpu_choice
import random
game()
def game ():
p_choice = raw_input("What do you choose?")
cpu_random = random.randint(1,3)
cpu_choice = cpu_random
if cpu_random == 1:
cpu_choice = "Rock"
elif cpu_random == 2:
cpu_choice = "Paper"
elif cpu_random == 3:
cpu_choice = "Scissors"
if cpu_choice == p_choice:
print ("Tie!")
if p_choice == "Rock" and cpu_choice == "Paper":
print ('''Uh Oh. You lost
Try Again next time!''')
play_again = raw_input ("Would you like to play again?")
if p_choice == "Rock" and cpu_choice == "Scissors":
print ("You Win!!!")
play_again = raw_input ("Would you like to play again?")
if p_choice == "Paper" and cpu_choice == "Rock":
print ("You Win!!!")
play_again = raw_input ("Would you like to play again?")
if p_choice == "Paper" and cpu_choice == "Scissors":
print ('''Uh Oh. You lost
Try Again next time!''')
play_again = raw_input ("Would you like to play again?")
if p_choice == "Scissors" and cpu_choice == "Rock":
print ('''Uh Oh. You lost
Try Again next time!''')
play_again = raw_input ("Would you like to play again?")
if p_choice == "Scissors" and cpu_choice == "Paper":
print ("You Win!!!")
play_again = raw_input ("Would you like to play again?")
def play_again ():
if play_again == "Yes" or "yes":
game()
if play_again == "No" or "no":
print ("Okay. See you next time")
exit()
当它问我:
Rock
然后我得到一个错误代码,如下所示:
NameError: name 'Rock' is not defined
对不起,如果这是重复的,但无论如何,谢谢。我在上面找不到别的东西了
一些可能有用的东西:
我正在使用一个名为spyder2的程序进行编码。只要你知道,请帮忙
编辑:
错误的完整命令行:
runfile('/home/ubuntu-meyer9095/rockpaperscissors.py', wdir='/home/ubuntu-meyer9095')
What do you choose?Rock
Traceback (most recent call last):
File "<ipython-input-7-d0814fe6acfd>", line 1, in <module>
runfile('/home/ubuntu-meyer9095/rockpaperscissors.py', wdir='/home/ubuntu-meyer9095')
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/home/ubuntu-meyer9095/rockpaperscissors.py", line 6, in <module>
game()
File "/home/ubuntu-meyer9095/rockpaperscissors.py", line 9, in game
p_choice = raw_input("What do you choose?")
File "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/ipkernel.py", line 364, in <lambda>
input = lambda prompt='': eval(raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'Rock' is not defined
runfile('/home/ubuntu-meyer9095/rockpaperscissors.py',wdir='/home/ubuntu-meyer9095')
你选择什么?摇滚乐
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/home/ubuntu-meyer9095/rockpaperscissors.py',wdir='/home/ubuntu-meyer9095')
文件“/usr/lib/python2.7/dist packages/spyderlib/widgets/externalshell/sitecustomize.py”,第699行,在runfile中
execfile(文件名、命名空间)
文件“/usr/lib/python2.7/dist packages/spyderlib/widgets/externalshell/sitecustomize.py”,第81行,在execfile中
execfile(文件名,*其中)
文件“/home/ubuntu-meyer9095/rockpaperscissors.py”,第6行,在
游戏()
文件“/home/ubuntu-meyer9095/rockpaperscissors.py”,游戏第9行
p_choice=原始输入(“您选择什么?”)
文件“/usr/lib/python2.7/dist packages/IPython/kernel/zmq/ipkernel.py”,第364行,在
输入=lambda提示符='':评估(原始输入(提示符))
文件“”,第1行,在
NameError:未定义名称“Rock”
在python 3.x中,“raw_input”函数已过时,最好只使用“input()”,然后在定义“game()”函数之前调用它,因此代码不知道要查找什么。第三,Play_reach()函数从未使用过,因为您从未像调用“game()”那样调用它。我建议您完全重构代码,并查看有关函数的本教程。不过这个项目很酷 在你的代码中,这似乎不是问题,但在IPython中。你是否在某处编写了raw\u input=input
?没有做任何事情导致了另一个错误我认为Python环境存在问题