有任何改进此功能的提示吗?希望减少约5-10行

时间:2018-04-24 13:45:00

标签: python python-3.x

我正在学习python课程并被要求定义一个函数。虽然我确实得到了正确的功能,并且它适用于课程提供的每个测试用例并且我自己尝试过,但我想知道是否以及在哪里可以减少代码(粘贴在下面)。课程提要说它可以写成15-20行,我的是27。谢谢!

def playGame(wordList):
    """
    Allow the user to play an arbitrary number of hands.

    1) Asks the user to input 'n' or 'r' or 'e'.
      * If the user inputs 'n', let the user play a new (random) hand.
      * If the user inputs 'r', let the user play the last hand again.
      * If the user inputs 'e', exit the game.
      * If the user inputs anything else, tell them their input was invalid.

    2) When done playing the hand, repeat from step 1
    """
    draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")
    while draw != 'n':
        if draw == 'r':
            print("You have not played a hand yet. Please play a new hand first!")
            print('')
            draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")
        elif draw == 'e':
            break
        else: 
            print("Invalid command.")
            print('')
            draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")
    while True:
        if draw == 'n':
            hand = dealHand(HAND_SIZE)
            playHand(hand, wordList, HAND_SIZE)
            print('')
            draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")
        elif draw == 'r':
            playHand(hand, wordList, HAND_SIZE)
            print('')
            draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")
        elif draw == 'e':
            return
        else:
            print("Invalid command.")
            print('')
            draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")

1 个答案:

答案 0 :(得分:1)

虽然这是@TomDalton建议的https://codereview.stackexchange.com/的问题,但我确实看到了一个机会,可以帮助您理解像这样的程序中的控制流程。首先是更新的代码:

import random

HAND_SIZE = 5

def deal_hand(wordlist, size):
    return random.sample(wordlist, size)

def play_hand(hand):
    print("Words: {}".format(", ".join(hand)))

def play_game(wordlist):
    hand = None
    while True:
        draw = input("Enter n to deal a new hand, r to replay the last hand, or e to end the game: ")
        if draw == 'r':
            if not hand:
                print("You have not played a hand yet. Please play a new hand first!")
                continue
            play_hand(hand)
        elif draw == 'n':
            hand = deal_hand(wordlist, HAND_SIZE)
            play_hand(hand)
        elif draw == 'e':
            break
        else:
            print("Invalid command")

if __name__ == "__main__":
    words = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
    play_game(words)

花点时间阅读一下,看看是否有意义。

基本思想是我们要做一个非终止循环(while True)。在每次用户输入后,我们可以决定做什么(r, n and e)。您应该注意到,在原始代码中,eunknown的案例完全相同。您还有一个循环,一直持续到n为止。我认为你最终以这种方式结束了,因为当你输入的第一个选择(当没有手重复时)你想要处理r的情况。但是你可以在单个if内检查这个案例:要么告诉用户他们需要先做n,要么在已经生成一手牌的情况下重复最后一手牌。

关键是将您在顶部的评论(doc)转换为代码。此实现更准确地匹配文本描述。