我正在編寫一個編程作業,並且遇到了一個小小的障礙,我正在爲一個岩石,紙張,剪刀遊戲工作。該程序假設由用戶選擇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()
只是使用返回而不是sys.exit – 2014-10-19 23:14:13
他檢查玩家的選擇是否是在由main調用的函數中是4,所以不需要退出?顯然,最好的辦法是檢查main(),但是用他的結構不是這樣嗎? – Parker 2014-10-19 23:23:56
這正是我所需要的。謝謝! – 2014-10-20 01:12:45