zoukankan      html  css  js  c++  java
  • Python学习之edx6001-week4

    今天完成了week4的ps4b部分,代码如下:

    感觉好多重复代码,目前还没想到如何删繁就简,将就看吧。后续继续优化改进。

     1 def playGame(wordList):
     2     """
     3     Allow the user to play an arbitrary number of hands.
     4  
     5     1) Asks the user to input 'n' or 'r' or 'e'.
     6         * If the user inputs 'e', immediately exit the game.
     7         * If the user inputs anything that's not 'n', 'r', or 'e', keep asking them again.
     8 
     9     2) Asks the user to input a 'u' or a 'c'.
    10         * If the user inputs anything that's not 'c' or 'u', keep asking them again.
    11 
    12     3) Switch functionality based on the above choices:
    13         * If the user inputted 'n', play a new (random) hand.
    14         * Else, if the user inputted 'r', play the last hand again.
    15       
    16         * If the user inputted 'u', let the user play the game
    17           with the selected hand, using playHand.
    18         * If the user inputted 'c', let the computer play the 
    19           game with the selected hand, using compPlayHand.
    20 
    21     4) After the computer or user has played the hand, repeat from step 1
    22 
    23     wordList: list (string)
    24     """
    25     hand={}
    26     while True:
    27         tips=input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
    28         if tips not in ('n','r','e'):
    29             print('Invalid command.please try again.')
    30         else:
    31             if tips=='e':
    32                 break
    33             elif tips=='r':
    34                 if hand == {}:
    35                     print('You have not played a hand yet. Please play a new hand first!')
    36                 else:
    37                     while True:
    38                         uc = input('Enter u to have yourself play, c to have the computer play:')
    39                         if uc == 'c':
    40                             compPlayHand(hand, wordList, HAND_SIZE)
    41                             break
    42                         elif uc == 'u':
    43                             playHand(hand, wordList, HAND_SIZE)
    44                             break
    45                         else:
    46                             print('Invalid command.')
    47             else:
    48                 hand=dealHand(HAND_SIZE)
    49                 while True:
    50                     uc=input('Enter u to have yourself play, c to have the computer play:')
    51                     if uc=='c':
    52                         compPlayHand(hand,wordList,HAND_SIZE)
    53                         break
    54                     elif uc=='u':
    55                         playHand(hand, wordList, HAND_SIZE)
    56                         break
    57                     else:
    58                         print('Invalid command.')

    以上,就酱紫。

  • 相关阅读:
    音乐商店
    sort函数
    优先队列
    畅通工程 并查集,最小生成树,最短路径
    线段树(segment tree )
    bfs
    完全背包
    【Matlab】向图像域添加噪声/高斯/均匀/伽马/指数/椒盐
    【手帐】Bullet Journal教程
    【Matlab】取整函数:fix/round/floor/ceil
  • 原文地址:https://www.cnblogs.com/frank1126lin/p/8806573.html
Copyright © 2011-2022 走看看