Python 3 剪刀石头布问题
我正在做一个石头、剪刀、布的游戏作为编程作业,但遇到了一些问题。这个程序应该让用户选择四个选项中的一个:1) 石头,2) 布,3) 剪刀,4) 退出。玩家选择一个选项后,程序会显示电脑的选择,并宣布谁是赢家,然后询问你是否想再玩一局。如果选择“y”,就会返回主菜单选择另一个选项,选择其他任何东西则会显示赢了多少局、输了多少局以及平局的局数。如果玩家选择4,程序应该显示“正在退出程序...”并展示游戏结果。
我遇到的问题有:
第一次选择后,赢家会被显示,程序会返回主菜单。如果你进行第二次选择,程序会告诉你电脑选择了什么,然后问你是否想再玩一次。选择“y”会让你回到主菜单,但电脑的选择不会改变,无论你选择什么,游戏的结果总是和第一次一样。如果你选择不再玩,赢、输和平局的局数会显示出来(这部分似乎是正常工作的)。
退出选项会让你返回主菜单,而不是显示游戏结果。我不太确定应该把那个判断语句放在哪里。
如果能帮我解决这些问题,我会很感激。
谢谢
#import module
import random
def main():
#create a variable to control the loop
play_again = 'y'
#create a counter for tied games, computer games and player games
tied_games = 0
computer_games = 0
player_games = 0
#display opening message
print("Let's play rock, paper scissors!")
computer_choice = process_computer_choice()
player_choice = process_player_choice()
winner = determine_winner(player_choice, computer_choice)
#setup while loop for playing multiple games
while play_again == 'y' or play_again == 'Y':
process_computer_choice()
process_player_choice()
#use a if else statement to print the computers choice
if computer_choice == 1:
print('computer chooses rock.')
elif computer_choice == 2:
print('computer chooses paper.')
else:
print('computer chooses scissors.')
#call the determine winner function
determine_winner(player_choice, computer_choice)
#check who won the game and add 1 to the correct counter
if winner == 'computer':
computer_games += 1
elif winner == 'player':
player_games += 1
else:
tied_games += 1
#ask the user if they would like to play again
play_again = input('would you like to play again? (enter y for yes): ')
#display number of games that were won by the computer, the player and that were tied
print()
print('there was', tied_games, 'tied games.')
print('the player won', player_games, 'games.')
print('The computer won', computer_games,'games.')
#define the process computer function
def process_computer_choice():
#setup computer to select random integer between 1 and 3
choice1 = random.randint(1, 3)
#return the computers choice
return choice1
#define the process player function
def process_player_choice():
#add input for players choice
print()
print(' MENU')
print('1) Rock!')
print('2) Paper!')
print('3) Scissors!')
print('4) Quit')
print()
player_choice = int(input('Please make a selection: '))
#add if statement for quit option
if player_choice == 4:
print('Exiting program....')
#validate if the user enters a correct selection
while player_choice != 1 and player_choice != 2 and player_choice != 3 and player_choice != 4:
#print a error message if the wrong selection is entered
print('Error! Please enter a correct selection.')
player_choice = int(input('Please make a selection: '))
#return the players choice
return player_choice
#define the determine winner function
def determine_winner(player_choice, computer_choice):
#setup if else statements for each of the 3 computer selections
if computer_choice == 1:
if player_choice == 2:
print('Paper wraps rock. You win!')
winner = 'player'
elif player_choice == 3:
print('Rock smashes scissors. The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 2:
if player_choice == 1:
print('Paper wraps rock. The computer wins!')
winner = 'computer'
elif player_choice == 3:
print('Scissors cut paper. You win!')
winner = 'player'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 3:
if player_choice == 1:
print('Rock smashes scissors. You win!')
winner = 'player'
elif player_choice == 2:
print('Scissors cut paper. The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
main()
2 个回答
你没有再次给 computer_choice
或 player_choice
赋值,而是直接使用它们的值。
while play_again == 'y' or play_again == 'Y':
process_computer_choice()
process_player_choice()
应该是这样的:
while play_again == 'y' or play_again == 'Y':
computer_choice = process_computer_choice()
player_choice = process_player_choice()
至于退出的部分,只需要在选择退出时使用 break。你需要在 process_player_choice 函数中提前返回,同时在 main 函数中也要做一些处理。
所以在 process_player_choice 函数中:
if player_choice == 4:
print('Exiting program....')
return
在 main 函数中:
player_choice = process_player_choice()if player_choice == 4:
return
winner = determine_winner(player_choice, computer_choice)
#setup while loop for playing multiple games
while play_again == 'y' or play_again == 'Y':
computer_choice = process_computer_choice()
player_choice = process_player_choice()
if player_choice == 4:
break
对于第一个问题,是因为你在循环开始之前就设置了电脑和玩家的选择,而且从来没有更新它们。你可以把循环开始的部分改成:
while play_again == 'y' or play_again == 'Y':
computer_choice = process_computer_choice()
player_choice = process_player_choice()
你也可以删掉循环前面那些检查输入和赢家的代码,因为在第一轮的时候,这些代码其实是多余的。
对于第二个问题,只需要在选择了4之后添加结果,像这样:
if player_choice == 4:
print('Exiting program....')
print('there was', tied_games, 'tied games.')
print('the player won', player_games, 'games.')
print('The computer won', computer_games,'games.')
sys.exit() # be sure you add 'import sys' to the beginning of your file
另外,你在主循环中的那行代码 determine_winner(player_choice, computer_choice)
缩进了,这样只有在电脑选择剪刀的时候才会被调用,所以你应该把它的缩进去掉 :)