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

2024-10-03 00:16:39 发布

您现在位置: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


Tags: theyouifemployeeitempriceprintenter
1条回答
网友
1楼 · 发布于 2024-10-03 00:16:39

我在运行代码时遇到有关浮点转换的错误。因此,我对您的代码进行了一些简化,并重新组织了部分代码。这里的核心思想是尝试用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"

[Input] abc

[Output] Nice try buddy...

[Input] 24

[Output] Welcome Guest your emplyee # is:24 Welcome Kobe

[Input] Kobe24

[Output] Nice try buddy...

相关问题 更多 >