python-列印簡單公司員工資訊表

來源:互聯網
上載者:User

標籤:else   try   員工   輸出   counter   print   方法   port   格式化輸出   

python-列印簡單公司員工資訊表要求,輸入name不為空白,輸入次數最多3次,3次後跳出程式;

知識點:

raw_input

str轉int

whil

if elif else continue break 

for

匯入模組

引用變數值

格式化輸出

 

vim 

#!/usr/bin/env pythonimport sysuser_name = "carson"this_year = 2014counter  = 0while True:    if counter < 3:        name = raw_input("please input your name:").strip()        if len(name) == 0:            print "empty name , please input your name again!"            continue        elif name == user_name:            pass            print "welcome to login system!"        else:            print "%s is a not valid user, please try again!" % name            counter = counter + 1            continue        break    else:        print "Your input 3 times!"        sys.exit()    breakage = int(raw_input("How old are you?"))sex = raw_input("please input your sex:")hobby = raw_input("Do you hava any hobbies?")information =  ‘‘‘Information of company staff     Name :%s    Age  :%d    Sex  :%s    Hobby:%s    ‘‘‘ % (name,age,sex,hobby)print information
輸入3次,退出程式!# python carson.by please input your name:aa is a not valid user, please try again!please input your name:bb is a not valid user, please try again!please input your name:cc is a not valid user, please try again!Your input 3 times!
輸入空白字元,提示一直輸入# python carson.by please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!please input your name:empty name , please input your name again!

輸入正確:

please input your name:carsonwelcome to login system!How old are you?23please input your sex:MDo you hava any hobbies?footballInformation of company staff     Name :carson    Age  :23    Sex  :M    Hobby:football

    

 

在判斷輸入次數的還有一種方法是利用for迴圈:

#!/usr/bin/env pythonimport sysuser_name = "carson"while True:            name = raw_input("please input your name:").strip()        if len(name) == 0:            print "empty name , please input your name again!"            continue        for i range(1,3):            name = raw_input("please input your name:").strip()            if name == user_name:                pass                print "welcome to login system!"            else:                print "%s is a not valid user, please try again!" % name                continue            break        else:            print "Your input 3 times!"            sys.exit()        breakage = int(raw_input("How old are you?"))sex = raw_input("please input your sex:")hobby = raw_input("Do you hava any hobbies?")information =  ‘‘‘Information of company staff     Name :%s    Age  :%d    Sex  :%s    Hobby:%s    ‘‘‘ % (name,age,sex,hobby)print information

 

python-列印簡單公司員工資訊表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.