当我的程序继续返回相同的内容时,允许输入字母和数字

时间:2017-08-17 03:02:31

标签: python

employee = float(raw_input('Employee code number or 0 for guest:'))

if employee == 24:
     print "Welcome Kobe Bryant"
     print """

                                                                           ________________   
                                                                __________/________________\_________
                                                               //                                   \\
                                                              //                                     \\
                                                             ||                                      ||
                                                          ||                                      ||
                                                             ||                                      ||
                                                             ||                                      ||
                                                             |      _ \\  //  /\          _  _____     |
                                                             | _   ^  ) \\//  //\\    /\\   //|__ __/    |
                                                             |\  ) \\ \\  \/  //--\\  // \\ //   //       |
                                                             | \  ) \\ \\ || //    \\//   \\/   //        |
                                                             |  \/                                    |
         ===///    , ,-. ,-   .-, , , ,-  ,-  ,  ,- ,-       |       -------          ,-----          |
         --///    /_ |_/ _)  /--|/|/ |_] /=_ /_ /=_ _)       |     /  ___   \        / /|  |          |
       ___///    __    __  __  ______  ____    ___           |    / /     \  \      / / |  |          |
         ///  --/||  --//  /   //   / _//  )) (|  /          |   /_/      /  /     / /  |  |          |
     ---///   /' ||  _// //   //__    //_ //   \\             |          /  /      / /   |  |          |
     --///- -/---||  // ||   //     _//  ||     \\     ;      |         /  /      / /____|  |___       |
______/// __/_  _||_//   ||_//___/ _//   |\ /-._//    /      |       /  /       |_______    ___|      |
    _///     ,                            \\       .'         |     /   /_______         |  |          |
 ___///_____/                              `;=__.-'          |    |____________|        |__|          |
                                                             |                                        |
                                                             |                                        |
                                                             |                                        |
                                                             |________________________________________|
                                                             |________________________________________|



"""

if employee < 23:
    print""
    print"""\t\tWelcome Guest
Please Enter Item Price! If you enter a letter the program will crash! """

if employee > 24:
    print ""
    print"""\t\tWelcome GAVIN WHITFORT
Please Enter Item Price! If you enter a letter the program will crash!
"""

if employee == 23:
    print " "
    print """\t\t     WELCOME THE GOAT MICHAEL JORDAN"""
    print """         (Greatest of All Time)
    *                        CHICAGO                        *
   ***                        BULLS                        ***
   ****                        23                         ****
   *   ******                                       ******   *
   *         ***************************************         *
    **            *       ______*______       *            **
      *****      *        ______*______        *      *****
           *******        ______*______        *******
        ***       ****          *         ****        ***
            ****** *oo*******  ***  *******oo* ******
                 *   *o********* *********o*   *
                  *   *                   *   *
                   *   *   _         _   *   *
                    *   *  -_       _-  *   *
                     *  *   -_     _-   *  *
                      * *     -___-     * *
                       *                 *
                      *********************
                      *      **   **      *
                        *    **   **    *
                          *  _-----_  *
                            *********

                    ************                      .                             
               .e$**%    $    %%*$e.               
             z$**.       $         $$c                        
           z$*   *.      $       .P  ^$c                      
          d*      *      $      z       b                     
         $*        b     $     4%       ^$                    
        d%         *     $     P         '$                   
       .$           F    $    J           $r                  
       4L...........b....$....$...........J$                  
       $F           F    $    $           4$                  
       4F          4F    $    4r          4P                  
       ^$          $     $     b          $%                  
        3L        .F     $      r        JP                   
         *c       $      $       3.     z$                    
          *b     J       $        3r   dP                     
           ^$c  z%       $         c z$7                      
              *$L        $        .d$6                        
                 *$ee..  $  ..ze$P                  
                    ***********        """      





if(employee.isalpha()):
    print"Nice try buddy"
    print"Welcome BIG_OLD_BUDDY"
else:
    print "Welcome Guest"
    print "your emplyee # is:" + str(employee)

该程序在不考虑输入的情况下继续打印欢迎GAVIN WHITFORT。

1 个答案:

答案 0 :(得分:0)

我收到有关运行代码的浮点转换的错误。因此,我对您的代码进行了一些简化,并重新组织了部分代码。这里的核心思想是尝试使用int()解析你的输入,如果异常是caguht(即非数字字符输入),则执行其他操作

employee = raw_input('Employee code number or 0 for guest:')

try:
    employee_id = int(employee)
    print "Welcome Guest"
    print "your emplyee # is:" + str(employee)
    if employee_id == 24:
        print "Welcome Kobe"
    elif employee_id < 23:
        print "Guest"
    elif employee_id > 24:
        print "Gavin"
    elif employee_id == 23:
        print "Jordan"
except:
    print"Nice try buddy"
    print"Welcome BIG_OLD_BUDDY"

我建议提取一个函数,使其更清晰。

def welcome_employee_by_id(employee_id):
    if employee_id == 24:
        print "Welcome Kobe"
    elif employee_id < 23:
        print "Guest"
    elif employee_id > 24:
        print "Gavin"
    elif employee_id == 23:
        print "Jordan"
    # Try to reorganize the order and add border conditions
    # to make it more clear

employee = raw_input('Employee code number or 0 for guest:')

try:  # this deals with digit input
    employee_id = int(employee)
    print "Welcome Guest"
    print "your emplyee # is:" + employee
    welcome_employee_by_id(employee_id)
except: # deals with input if it cannot be converted to int
    print "Nice try buddy"
    print "Welcome BIG_OLD_BUDDY"
  

[输入] abc

     

[输出]很好的尝试好友...

     

[输入] 24

     

[输出]欢迎游客   你的emplyee#是:24   欢迎神户

     

[输入] Kobe24

     

[输出]很好的尝试好友...