简体   繁体   English

如何纠正我在石头剪刀布游戏中的错误

[英]How to correct my errors in my Rock, Paper, Scissors game

I have a problem figuring out how to define rock and determine_winner I'm hoping someone could check out what I have and tell me were i'm going wrong.我在弄清楚如何定义摇滚和确定赢家时遇到了问题,我希望有人可以检查我所拥有的并告诉我我做错了什么。 Also sometimes I get the program to run when I put it def main(): but it makes it spam in loop instead of hitting yes to play the game.有时,当我将它放入 def main(): 时,我会运行程序,但它会使其成为垃圾邮件,而不是点击 yes 来玩游戏。 Also am wondering if any of my indentations are wrong and if that could be the problem at hand.也想知道我的任何缩进是否错误,这是否可能是手头的问题。

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

You have to understand that a computer program goes top-down.您必须了解计算机程序是自上而下的。 Meaning that if you put this particullar code outside of main() (or any function in general) this happens:这意味着如果你把这个特定的代码放在main() (或任何一般的函数main()之外,会发生这种情况:

Essentially you assign the variable play_again the value 'y' and then the computer keeps on checking if the play_again is still y, which it is.本质上,您将变量play_again赋值为 'y',然后计算机继续检查play_again是否仍然是 y,它是。 You can think of it as the computer completely ignoring the function until it's "called", meaning you do the thing you did at the end of the file "main()".您可以将其视为计算机在“调用”之前完全忽略该函数,这意味着您执行在文件“main()”末尾所做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM