Python 3岩石,纸张,剪刀问题
我正在编写一个编程作业,并且遇到了一个小小的障碍,我正在为一个岩石,纸张,剪刀游戏工作。该程序假设由用户选择4个选项中的1个,1)摇滚,2)纸,3)剪刀和4)退出。一旦玩家选择了一个选项,计算机的选择就会显示出来,获胜者将被宣布,程序会询问你是否想玩另一个游戏。如果选择y,则返回主菜单选择另一个选项,其他任何选项都会显示赢得的比赛数量,丢失的比赛以及比赛结束时的比赛数量。如果玩家选择4,则该程序应该说“退出程序...”,并且应该显示游戏结果。Python 3岩石,纸张,剪刀问题
这里是我的问题:
一旦你做出了第一个选择,显示赢家和程序返回到主菜单。如果您再次选择,它会通知您电脑选择的内容,然后询问您是否想再次播放。 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()
对于问题1,这是因为您在循环之前设置的计算机和球员的选择,从不更新它们。你的循环的开始更改为:
while play_again == 'y' or play_again == 'Y':
computer_choice = process_computer_choice()
player_choice = process_player_choice()
另外,还可以检查输入的赢家,因为它是第一轮技术上冗余环路之前删除的代码行。
对于问题2,只需添加结果选择了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)
缩进所以它才会被调用,如果计算机选择剪刀,所以你应该unindent::)
您还没有分配给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()
至于退出只是在退出选择突破。您必须尽早从process_player_choice返回,并在main中执行一些操作。
所以在process_player_choice:
if player_choice == 4:
print('Exiting program....')
return
,并在主: 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
'如果player_choice == 4:'不在循环中,所以你不能使用break,你会使用return – 2014-10-19 23:17:11
@PadraicCunningham,'while play_again =='y'或者play_again == 'Y':'不是一个循环? – 2014-10-19 23:27:39
我的解决方案意味着在第一轮退出时什么都不会做,但之后会正常工作。这可能不是理想的。 – 2014-10-19 23:31:27
只是使用返回而不是sys.exit – 2014-10-19 23:14:13
他检查玩家的选择是否是在由main调用的函数中是4,所以不需要退出?显然,最好的办法是检查main(),但是用他的结构不是这样吗? – Parker 2014-10-19 23:23:56
这正是我所需要的。谢谢! – 2014-10-20 01:12:45