我是一名初学程序员,我只是不明白为什么我的摇滚剪刀程序不起作用,之前有效但我尝试添加一个步骤,如果用户按下r则重复。我在哪里搞砸了?
第一个程序是在我尝试添加重启之前。它按照我的预期工作。
from random import randint
options = {1:"Rock",2:"Paper",3:"Scissors"}##This list is for the raw data to output
number = randint(1,3)###Random selection of rock paper or scissors
computer = options[number]
print "Rock, Paper, Scissors Key"
print "1 = Rock"
print "2 = Paper"
print "3 = Scissors"
human = input("Enter what you want. ")
print "Player chose",options[human]
def end(computer,human):
if computer == human:
return "Draw"
elif computer == 1 and human == 2:
return "Computer chose Rock, Player has won!"
elif computer == 1 and human == 3:
return "Computer chose Rock, Player has lost."
elif computer == 2 and human == 1:
return "Computer chose Paper, Player has won!"
elif computer == 2 and human == 3:
return "Computer chose Paper, Player has lost"
elif computer == 3 and human == 1:
return "Computer chose Scissors, Player has won!"
elif computer == 3 and human == 2:
return "Computer chose Scissors, Player has lost."
else:
return "ERROR"
print end(number, human)
input("To finish press any key.")
下一个程序是我尝试添加重启功能的时候。这个不再适用了。为什么呢?
from random import randint
i = 1
while i == 1:
options = {1:"Rock",2:"Paper",3:"Scissors"}
number = randint(1,3)###Random selection of rock paper or scissors
computer = options[number]
print "Rock, Paper, Scissors Key"
print "1 = Rock"
print "2 = Paper"
print "3 = Scissors"
human = input("Enter what you want. ")
print "Player chose",options[human]
def end(computer,human):
if computer == human:
return "Draw"
elif computer == 1 and human == 2:
return "Computer chose Rock, Player has won!"
elif computer == 1 and human == 3:
return "Computer chose Rock, Player has lost."
elif computer == 2 and human == 1:
return "Computer chose Paper, Player has won!"
elif computer == 2 and human == 3:
return "Computer chose Paper, Player has lost"
elif computer == 3 and human == 1:
return "Computer chose Scissors, Player has won!"
elif computer == 3 and human == 2:
return "Computer chose Scissors, Player has lost."
else:
return "ERROR"
print end(number, human)
restart_var = input("Press R to restart and Enter to exit")
def restart(x):
if x == r:
i = 1
else:
i = 0
restart(restart_var)
答案 0 :(得分:4)
问题在于:
if x == r:
您将x
与未知变量r
进行比较。
正确的做法是与字符串“r'进行比较。因为input
返回字符串。
if x == 'r':
答案 1 :(得分:1)
当您找到用户从输入中选择的选项时,第一个脚本中的内容会出错。 input
会给你一个字符串,而不是一个数字。您可以使用int(input("Enter what you want"))
这样可以正常工作。接下来要做的是允许重新启动。您所要做的就是创建一个启动程序的功能。像这样:
def start():
print("Rock, Paper, Scissors Key")
print("1 = Rock")
print("2 = Paper")
print("3 = Scissors")
number = randint(1,3)
computer = options[number]
human = int(input("Enter what you want. "))
print("Player chose",options[human])
现在在end
函数中调用start
函数:
print(end(number, human))
现在你只需检查最后一个输入,看看用户是否想要重启。
if input("Press R to restart and Enter to exit").lower() == "r":
start()
所以你的新最终脚本如下所示:
from random import randint
options = {1:"Rock",2:"Paper",3:"Scissors"}##This list is for the raw data to output
def end(computer,human):
if computer == human:
return "Draw"
elif computer == 1 and human == 2:
return "Computer chose Rock, Player has won!"
elif computer == 1 and human == 3:
return "Computer chose Rock, Player has lost."
elif computer == 2 and human == 1:
return "Computer chose Paper, Player has won!"
elif computer == 2 and human == 3:
return "Computer chose Paper, Player has lost"
elif computer == 3 and human == 1:
return "Computer chose Scissors, Player has won!"
elif computer == 3 and human == 2:
return "Computer chose Scissors, Player has lost."
else:
return "ERROR"
def start():
#display the game info
print("Rock, Paper, Scissors Key")
print("1 = Rock")
print("2 = Paper")
print("3 = Scissors")
#get a random number for the computer
number = randint(1,3)
computer = options[number]
#get human input
human = int(input("Enter what you want. "))
#print what the human chose
print("Player chose",options[human])
#run the calculator
print(end(number, human))
if input("Press R to restart and Enter to exit ").lower() == "r":
print('\n'*3) #print some line breaks
start()
#start the game
start()
注意我是在Python 3中写的。如果您使用的是Python 2,请将input
更改为raw_input
。
答案 2 :(得分:1)
首先,第二个示例在while
之后只有两个分配缩进。这将导致无限循环,因为破坏其条件的事情是在程序的最后。
其次,在Python 2中,input()
解析表达式(这使得它非常危险),这就是它在Python 3中被替换的原因。这可以按照你想要的数字而不是字母来实现,它会查找变量。你可以滥用它来使你的代码工作:
r="Willy Wonka's shoes didn't taste so well."
R=r
val=input("Can you type R? ")
if val in (r, R):
print "You could! Or maybe you typed my secret phrase in quotes. I don't know!"
值得注意的是,这意味着如果用户只是点击Enter就会得到一个SyntaxError异常 - 因为空字符串不是有效的Python表达式。键入关键字也是一样,未定义的变量会使它们成为NameError。
要使用的函数称为raw_input
,并且始终生成一个字符串(在Python 3中,这就是输入所做的)。您可以将其包装在一些代码中,以使其更有用:
def read_int(question=""):
val=None
while val is None:
s=raw_input(question)
try:
val=int(s,0)
except ValueError, e:
pass
return val
def confirm_letter(question="", letter="y"):
s=raw_input(question)
return s.lower().startswith(letter)
说到功能,我想你可以使用更多功能。像重启一样的控制流程更容易阅读(和编辑!),如果,而不是让整个程序介于两者之间,它看起来像:
while True:
play_game()
if not confirm_letter("Type R to restart: ", "r"):
break
作为进一步的练习,我很想为玩家的行为写一个类,它会知道如何比较(看谁赢了)并代表他们的名字。
答案 3 :(得分:0)
human=input("Enter what you want:")
是一种字符串类型。试试print options[int(human)]
或将人类视为human=int(input("Enter what you want:"))
在end
函数中,您似乎正在将strings
与integers
进行比较。更好的实施将有所帮助。您可以尝试比较字符串。 computer="Rock" and options[human]="Scissors" return "Computer chose Rock, Player has lost"