Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 巨蟒3:石头、布、剪刀发行_Python - Fatal编程技术网

Python 巨蟒3:石头、布、剪刀发行

Python 巨蟒3:石头、布、剪刀发行,python,Python,我正在做一个石头、布、剪刀的游戏来做编程作业,我遇到了一个小麻烦。假设该程序由用户运行,用户选择4个选项中的1个,1)石头,2)布,3)剪刀和4)退出。一旦玩家选择了一个选项,将显示计算机选项,并宣布获胜者,程序将询问您是否想玩另一个游戏。如果选择y,则返回主菜单选择另一个选项,其他任何选项都将显示赢得、输掉的比赛数量以及平局结束的比赛数量。如果玩家选择4,程序应显示“退出程序…”,并显示游戏结果 以下是我的问题: 第一次选择后,将显示赢家,程序将返回主菜单。如果您进行第二次选择,它将通知您计算

我正在做一个石头、布、剪刀的游戏来做编程作业,我遇到了一个小麻烦。假设该程序由用户运行,用户选择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()
    

    您不再分配给
    计算机选项
    玩家选项
    ,而是使用它的值

    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()
    
    至于戒烟,就打断你的选择吧。你必须从过程中提前回来,也要在主要方面做些事情

    因此,在选择过程中:

    if player_choice == 4:
        print('Exiting program....')
        return
    
    总的来说: 玩家选择=过程玩家选择()


    对于问题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\u winner(player\u choice,computer\u choice)
    中的行缩进,因此只有当计算机选择剪刀时才会调用它,因此您应该取消提示:)

    只需使用return而不是sys.exit检查main调用的函数中player choice是否为4,所以这不需要退出吗?显然,最好的方法是检入main(),但对于他的结构,这不是正确的吗?这正是我所需要的。谢谢<代码>如果player_choice==4:不在循环中,因此不能使用break,则可以使用return@PadraicCunningham,`while play_reach=='y'或play_reach=='y':`不是循环吗?我的解决方案是,在第一轮退出不会起任何作用,但之后会正常工作。它可能不是理想的。if语句在OP代码的while循环之外,将返回work@PadraicCunningham,您的第一个选择是正确的,return将起作用,但对于循环中的选择,break是正确的,因为它将在之后打印结果,然后退出。我还错过了添加输入函数的返回。
    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