[英]How to correct my errors in my Rock, Paper, Scissors game
我在弄清楚如何定義搖滾和確定贏家時遇到了問題,我希望有人可以檢查我所擁有的並告訴我我做錯了什么。 有時,當我將它放入 def main(): 時,我會運行程序,但它會使其成為垃圾郵件,而不是點擊 yes 來玩游戲。 也想知道我的任何縮進是否錯誤,這是否可能是手頭的問題。
import random
def process_computer_choice():
return random.randint(1,3)
def process_player_choice():
pass
def main():
def computer_choice():
return random.choice([1,2])
play_again = 'y'
number_of_tied_games = 0
number_of_player_games = 0
number_of_computer_games = 0
print("Let's play the game of rock, Paper, Scissors.")
while play_again == 'y' or play_again == 'Y':
computer_choice=process_computer_choice()
player_choice=process_player_choice()
if computer_choice == 1:
print('the computer chooses rock.')
elif computer_choice == 2:
print('the computer chooses paper.')
else:
print('the computer chooses scissors.')
#display player choice
if player_choice == 1:
print('You choose rock.')
elif player_choice == 2:
print('You choose paper.')
else:
print('You choose scissors.')
result = determine_winner(player_choice, computer_choice)
if results == 'computer':
number_of_computer_games += 1
elif result == 'player':
number_of_player_games += 1
else:
number_of_tied_games += 1
play_again = input('Do you want to play again? (Enter y for yes): ')
print('There were', number_of_tied_games, 'tie games played.')
print('The computer won', number_of_computer_games, 'game(s).')
print('You won', number_of_player_games, 'game(s).')
def process_computer_choice():
choice1 = random.randint(1, 3)
def process_player_choice():
print('What is your choice? Enter 1 for rock, 2 for paper, or 3 for scissors: ',)
choice2 = int(input())
while choice2 != 1 and choice2 !=2 and choice2 != 3:
print('ERROR: the choice can only be 1, 2 or 3.')
choice2 = int(input("Please enter a correct choice:"))
return choice2
def determine_winner(player_choice, computer_choice):
if coputer_choice == 1:
if player_choice == 2:
print('Paper covers rock. You wins!')
winner = 'player'
elif player_choice == 3:
print('Rock crushes 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 covers rock. The computer wins!')
winner = 'computer'
elif player_choice == 3:
print(' Scissors cuts 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 cuts paper. The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
main()
play_again = 'y' # <- here's the issue
number_of_tied_games = 0
number_of_player_games = 0
number_of_computer_games = 0
print("Let's play the game of rock, Paper, Scissors.")
while play_again == 'y' or play_again == 'Y': # <-and here
您必須了解計算機程序是自上而下的。 這意味着如果你把這個特定的代碼放在main()
(或任何一般的函數main()
之外,會發生這種情況:
本質上,您將變量play_again
賦值為 'y',然后計算機繼續檢查play_again
是否仍然是 y,它是。 您可以將其視為計算機在“調用”之前完全忽略該函數,這意味着您執行在文件“main()”末尾所做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.