Python中的石头剪刀布
我正在尝试写一个Python程序,但我在获取分数时遇到了困难。我把它写成了一个返回值的函数,每次运行程序时,它似乎都会跳过获取分数的那一步,除非我加上一个else语句,这样它就会自动跳到else语句那里。
我会把完整的代码附在下面。
非常感谢任何帮助,我很感激!
这是我第一次在这个论坛发帖,如果我搞错了什么,我很抱歉。
#constants
Rock = 1
Paper = 2
Scissors = 3
#Define the main function
def main():
#set control loop
keep_going = 'Y'
#set counter to zero
computer_wins = 0
player_wins = 0
tie_score = 0
#call display message
display_message()
while keep_going == 'y' or keep_going == 'Y':
play_game()
#prompt user to keep going
keep_going = input('would you like to play again? (Y for Yes): ')
print('The computer won', computer_wins, 'times')
print('The player won', player_wins, 'times')
print('There were', tie_score, 'tie scores')
def play_game():
#get random input
computer = get_random()
#get the players input
play = get_play()
#validate input
if play == '1' or play == '2' or play == '3':
play == True
else:
play == False
print('Error: Invalid Entry')
play = input('Please enter 1 for Rock, 2 for Paper, or 3 for Scissors: ')
if play == computer:
print('Tie Score, Please try again')
tie_score += 1
else:
get_score(computer, play)
print('The computer chose:', computer)
print('The player chose: ', play)
#define display message
def display_message():
print('Welcome to Rock Paper Scissors, a game of chance to see who will')
print('outsmart the other. This game is Man VS Computer.')
print('The program will select a random integer and then ask you for an integer')
print('1 for Rock 2 for paper or 3 for Scissors. The program will then tell')
print('you who won the game.')
print('GOOD LUCK!')
print
print
def get_random():
import random
#generate random int
computer = random.randint(1, 3)
return computer
def get_play():
#prompt user to enter an integer 1, 2, or 3
play = input('Select 1 for Rock, 2 for Paper, or 3 for Scissors: ')
return play
def get_score(computer, play):
if computer == 1 and play == 2:
score = 'player wins'
print('Paper covers Rock, Player Wins')
#player wins
player_wins += 1
elif computer == 1 and play == 3:
score = 'computer wins'
print('Scissors cut Paper, Computer Wins')
#computer wins
computer_wins += 1
elif computer == 2 and play == 1:
score = 'computer wins'
print('Paper covers Rock, Computer Wins')
#computer wins
computer_wins += 1
elif computer == 2 and play == 3:
score = 'player wins'
print('Scissors cut Paper, Player Wins')
#player wins
player_wins += 1
elif computer == 3 and play == 1:
score = 'player wins'
print('Rock smashes Scissors, Player Wins')
#player wins
player_wins += 1
elif computer == 3 and play == 2:
score = 'computer wins'
print('Scissors cut Paper, Computer Wins')
#computer wins
computer_wins += 1
#call main function
main()
10 个回答
这里有几个快速的注意事项,都是我快速浏览代码时想到的:
在 get_score()
函数里,你可以加一个 else 条件来处理平局的情况,这样在 play_game()
函数里就不需要专门去检查了。
把 import random
移到文件的最上面。一般来说,导入的模块都是放在文件开头的。而且每次想要随机数时,不需要重复导入。
不太确定这是不是个笔误,因为 play
似乎总是一个整数,但你在 play_game()
里用到了 play == True
和 play == False
。如果你想让 play
只包含 True
或 False
,你应该用一个等号,比如 play = True
。不过这看起来不太合理,因为你在把 play
和 computer
比较,像是在比较整数一样。
另外,你在 get_score()
方法里想用 score
变量做什么呢?
哦,如果你让 get_score()
方法返回一些信息,告诉你谁赢了比赛,那会很有帮助。因为 computer_wins
和 player_wins
是在 main()
里定义的,所以在 get_score()
方法里是无法访问的。一个简单的方法是从 get_score()
返回一个整数。这是一种比较 C 风格的处理方式(返回 -1/0/1)。可以像这样写(伪代码):
def get_score():
score = 0
if computer wins:
score = -1
elif player wins:
score = 1
return score
winner = get_score()
if winner == 0:
print 'tie game'
elif winner == 1
print 'the player won'
else:
print 'the computer won'
我单独回答了你的问题,不过为了好玩,这里有一个简单的石头、剪刀、布游戏的代码可以看看。这个代码是为Python 2.x写的,可能在Python 3中无法运行,但对你或者将来有需要的人可能会有帮助。
# "Rock, Paper, Scissors" demo for Python 2.x
# by Dan Kamins
import random
ROCK = 1
PAPER = 2
SCISSORS = 3
NAMES = { ROCK: 'Rock', PAPER: 'Paper', SCISSORS: 'Scissors' }
WHAT_BEATS_WHAT = { ROCK: SCISSORS, PAPER: ROCK, SCISSORS: PAPER }
WIN_ACTIONS = { ROCK: 'crushes', PAPER: 'smothers', SCISSORS: 'cuts' }
score_player = 0
score_computer = 0
score_ties = 0
def main():
intro()
while main_loop():
pass
summary()
def intro():
print "Welcome to Rock, Paper, Scissors!"
def main_loop():
player = get_player_input()
computer = random.randint(1, 3)
check_result(player, computer)
return ask_play_again()
def check_result(player, computer):
global score_player, score_computer, score_ties
if player == computer:
print "Tie! Computer also chose {0}.".format(NAMES[computer])
score_ties += 1
else:
if WHAT_BEATS_WHAT[player] == computer:
print "Your massive {0} {1} the computer's {2}!".format(
NAMES[player], WIN_ACTIONS[player], NAMES[computer])
score_player += 1
else:
print "The computer's {0} {1} your pathetic {2}!".format(
NAMES[computer], WIN_ACTIONS[computer], NAMES[player])
score_computer += 1
def ask_play_again():
again = raw_input("Enter Y to play again: ")
return again in ('y', 'Y')
def get_player_input():
while True:
print
player = raw_input("Enter 1 for Rock 2 for paper or 3 for Scissors: ")
try:
player = int(player)
if player in (1,2,3):
return player
except ValueError:
pass
print "Please enter a number from 1 to 3."
def summary():
global score_player, score_computer, score_ties
print "Thanks for playing."
print "Player won: ", score_player
print "Computer won: ", score_computer
print "Ties: ", score_ties
if __name__ == '__main__':
main()
这段代码有很多问题,让人不知道从哪里开始说起(不过别灰心)……
首先,看起来你在用的是Python 3(主要是因为你用到了input
而不是raw_input
,还有你在打印时用的括号),这会限制你能得到的帮助。大多数人还是在用Python 2.6或2.7。不过,先不说这个……
接下来,关于你提问的主要问题有:
第一:你在处理玩家输入时用的是字符串(比如 '1', '2', '3'),而计算机选择的是数字(比如 1, 2, 3)。所以你需要以相同的方式进行比较。换句话说,不要这样:
if computer == 1 and play == 2:
你应该这样写:
if computer == 1 and play == '2':
第二:你试图在一个函数中引用另一个函数的变量,这样是行不通的。如果你想让computer_wins
等变量是全局的,你需要在全局范围内初始化它们,比如在你声明"#constants"之后,进入main
之前。然后在任何使用这些变量的函数中,你必须写global computer_wins
来表明它们是全局变量,而不是局部变量。
解决了这些问题后,代码应该会好一些,但你仍然需要做很多整理和继续努力!
坚持下去,很快这就会变得很自然了。