【问题标题】:Python 3 rock, paper, scissors issuePython 3 石头剪刀布问题
【发布时间】:2014-10-19 22:43:10
【问题描述】:

我正在为编程作业做一个石头、纸、剪刀游戏,但我遇到了一个小障碍。该程序假设由用户选择 4 个选项中的 1 个来运行,1) 岩石,2) 纸,3) 剪刀和 4) 退出。一旦玩家选择了一个选项,就会显示计算机选择并宣布获胜者,并且程序会询问您是否想玩另一个游戏。如果选择 y,您将返回主菜单选择另一个选项,其他任何内容都会显示赢、输的游戏数量以及以平局结束的游戏数量。如果玩家选择 4,程序应该会说“Exiting program...”,然后会显示游戏结果。

这是我的问题:

  1. 一旦您做出第一次选择,就会显示获胜者,并且程序会返回主菜单。如果您进行第二次选择,它将通知您计算机选择了什么,然后询问您是否想再次播放。 Y 会带你回到主菜单,电脑选择永远不会改变,无论你选择什么,游戏总是会以与第一个游戏相同的结果结束。如果您选择不再玩,则会显示赢、输和平局的数量(这似乎运行正常)。

  2. 退出选项将带您返回主菜单,而不是显示游戏结果。我不确定该 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()

【问题讨论】:

    标签: python


    【解决方案1】:

    对于问题 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) 是缩进的,因此只有在计算机选择剪刀时才会调用它,所以你应该取消缩进:)

    【讨论】:

    • 只使用 return 而不是 sys.exit
    • 他在 main 调用的函数中检查玩家选择是否为 4,所以这不需要退出吗?显然最好的方法是检查 main(),但是用他的结构会不会这样?
    • 这正是我所需要的。谢谢!
    【解决方案2】:

    您不是再次分配给computer_choiceplayer_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
    

    【讨论】:

    • if player_choice == 4: 不在循环中,所以你不能使用 break,你可以使用 return
    • @PadraicCunningham, ` while play_again == 'y' or play_again == 'Y':` 不是循环?
    • 我的解决方案意味着在第一轮退出不会做任何事情,但之后会正常工作。这可能不是理想的。
    • if 语句在 OP 代码中的 while 循环之外,return 将起作用
    • @PadraicCunningham,第一个选择是正确的,return 会起作用,但是对于循环中断中的选择是正确的,因为它会在之后打印结果然后退出。我也错过了从输入函数添加返回。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多