我正在制作一个摇滚,纸张,剪刀游戏,用于编程家庭作业,我遇到了一些障碍。该程序假设由用户选择4个选项中的1个,1)摇滚,2)纸,3)剪刀和4)退出。一旦玩家选择了一个选项,就会显示计算机选择并宣布获胜者,并且程序将询问您是否想要玩另一个游戏。如果你选择y,你会回到主菜单选择另一个选项,其他任何东西都会带来赢得的比赛数量,输掉比赛以及比赛结束的比赛数量。如果玩家选择4,程序应该说“退出程序......”并显示游戏结果。
以下是我的问题:
进行第一次选择后,将显示获胜者,程序将返回主菜单。如果您进行第二次选择,它将通知您计算机选择的内容,然后询问您是否要再次播放。 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()
答案 0 :(得分:2)
对于问题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)
中的行是缩进的,因此只有在计算机选择剪刀时才会调用它,所以你应该取消它:)
答案 1 :(得分:0)
您不能再次分配给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