当我刚开始时,我正在制作一个石头剪刀布游戏作为一个小项目,我必须经常使用相同的代码。最好的方法是什么,因为目前我已经将它复制并粘贴到了整个地方。它看起来很不整洁,而且我在验证输入时遇到了循环问题,不得不全部更改它是一件很痛苦的事情。
user_continue = raw_input("Would you like to play again? Y/N: ")
user_continue = user_continue.upper()
#Yes or no continue for the continue while loop to continue working until correct user input.
y_n_continue = False
while y_n_continue == False:
if user_continue == "Y" or user_continue == "YES" or user_continue == "N" or user_continue == "NO":
if user_continue == "Y" or user_continue == "YES":
continue_game = True
y_n_continue = True
elif user_continue == "N" or user_continue == "NO":
continue_game = False
y_n_continue = True
else:
print "Press Y or N"
y_n_continue = False
else:
print ""
如果我添加整个代码可能会更容易(有了修复,感谢 Anton。目前我收到错误 - TypeError:'bool' object is not callable。
我基本上是想让它循环游戏只要用户想要,同时还要验证输入以使一切尽可能防弹。
编辑 2 - 这是新代码,我有一些测试数据。
当我启动它时,系统会提示您在开始时输入 y/n。
您还必须在每场比赛后输入 y 或 n 两次。
如果您将“错误”数据输入到石头/纸/剪刀选择中,它将进入 y/n 选择
import random
def continue_game():
while True:
user_continue = raw_input("Would you like to play again? Y/N: ").upper()
if user_continue in ["Y", "YES", "N", "NO"]:
return user_continue in ["Y", "YES"]
else:
print "Press Y or N"
while continue_game():
#computers choice of rock, paper or scissors
computer_input = ["ROCK", "PAPER", "SCISSORS"]
computer_choice = random.choice(computer_input)
#users choice or rock, paper or scissors
user_input = raw_input("Choose rock, paper or scissors: ")
#Turns user input to upper case.
user_choice = user_input.upper()
if user_choice == "ROCK" or user_choice == "PAPER" or user_choice == "SCISSORS":
#Computer = ROCK
if computer_choice == "ROCK":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#Computer = PAPER
elif computer_choice == "PAPER":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#Computer = SCISSORS
elif computer_choice == "SCISSORS":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
else:
print "Something has gone wrong."
else:
print "Are you sure you entered that correctly?"
输出:
Would you like to play again? Y/N: y
Choose rock, paper or scissors: rock
You have chosen: ROCK
The computer has chosen: PAPER
You lose!
Would you like to play again? Y/N: y
continue
Would you like to play again? Y/N: y
Choose rock, paper or scissors: paper
You have chosen: PAPER
The computer has chosen: ROCK
You win!
Would you like to play again? Y/N: wer
Press Y or N
Would you like to play again? Y/N: dfg
Press Y or N
Would you like to play again? Y/N: y
continue
Would you like to play again? Y/N: y
Choose rock, paper or scissors: test
Are you sure you entered that correctly?
Would you like to play again? Y/N: y
Choose rock, paper or scissors: rock
You have chosen: ROCK
The computer has chosen: SCISSORS
You win!
Would you like to play again? Y/N: n
exit
Would you like to play again? Y/N: n
>>>
我知道我很痛苦,但这一切都值得赞赏。