我不知道为什么,但这让我感到震惊。
我的代码:
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的程序来编码。如果你知道plz的帮助。
编辑:
错误的完整命令行:
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
答案 0 :(得分:0)
在python 3.x中,“raw_input”函数已经过时,最好只使用“input()”,然后在定义它之前调用“game()”函数,因此代码不知道要找什么。第三,Play_Again()函数永远不会被使用,因为你没有像使用“game()”那样调用它。我建议您完全重构代码并查看有关函数Functions的本教程。很酷的项目!