【发布时间】:2014-10-19 22:43:10
【问题描述】:
我正在为编程作业做一个石头、纸、剪刀游戏,但我遇到了一个小障碍。该程序假设由用户选择 4 个选项中的 1 个来运行,1) 岩石,2) 纸,3) 剪刀和 4) 退出。一旦玩家选择了一个选项,就会显示计算机选择并宣布获胜者,并且程序会询问您是否想玩另一个游戏。如果选择 y,您将返回主菜单选择另一个选项,其他任何内容都会显示赢、输的游戏数量以及以平局结束的游戏数量。如果玩家选择 4,程序应该会说“Exiting program...”,然后会显示游戏结果。
这是我的问题:
一旦您做出第一次选择,就会显示获胜者,并且程序会返回主菜单。如果您进行第二次选择,它将通知您计算机选择了什么,然后询问您是否想再次播放。 Y 会带你回到主菜单,电脑选择永远不会改变,无论你选择什么,游戏总是会以与第一个游戏相同的结果结束。如果您选择不再玩,则会显示赢、输和平局的数量(这似乎运行正常)。
退出选项将带您返回主菜单,而不是显示游戏结果。我不确定该 if 语句应该放在哪里。
对于这些问题的任何帮助将不胜感激。
谢谢
#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()
【问题讨论】:
标签: python