python小練習

來源:互聯網
上載者:User

標籤:使用者登陸   判斷   切割   toolbar   tin   blog   UI   cmd   class   

1.

#使用while迴圈輸出1 2 3 4 5 6     8 9 10count=1while count < 11:    if count == 7:        count+=1        continue    print(count)    count+=1

2.

#輸出 1-100 內的所有奇數for j in range(1,100):    if j%2==0:        continue    print(j)

3.

#輸出 1-100 內的所有偶數for i in range(1,51):    i+=i    print(i)

4.

#求1-2+3-4+5 ... 99的所有數的和j = 1o = 0while j<=99:    if j%2==0:        o-=j    else:        o+=j    j+=1print(o)

5.

#使用者登陸(三次機會重試n = 0while n<3:    user=input(‘please enter your name:‘)    password=input(‘passwd:‘)    if user == ‘buer‘ and password == ‘1234.com‘:        print(‘login successful!‘)        while True:            shu = input(‘>>>:‘)            if shu == ‘quit‘:                n=3                break    else:        print(‘login fail‘)        n+=1

6.

#輸入quit退出迴圈while True:    sentence=input(‘請輸入內容:‘)    if sentence == ‘quit‘:        break    else:        print(sentence)

7.

#求1-100相加的和sum = 0for x in range(101):    sum+=xprint(sum)

8.

#輸入名字name = input(‘please your name:‘)print(‘hello,‘,name)

9.

#求奇數和偶數的和j = 0o = 0while j<=100:    if j%2==0:        o+=j    else:        o+=j    #print(j)    j+=2print(‘偶數求和:‘,o)j = 1o = 0while j<=100:    if j%2==0:        o-=j    else:        o+=j    #print(j)    j+=2print(‘奇數求和:‘,o)

10.

#登入使用者buer,密碼123,進入使用者輸quit退出迴圈tag=Truewhile tag:    name=input(‘please your name:‘)    pswd=input(‘please your password:‘)    if name == ‘buer‘ and pswd == ‘123‘:        print(‘login successful!‘)        while tag:            cmd=input(‘==>>:‘)            if cmd == ‘quit‘:                tag=False                continue            print(cmd)

11.

#乘法口訣表for i in range(1,10):    for j in range(1,i+1):        print(‘%s*%s=%s‘ %(i,j,i*j),end=‘ ‘)    print()

12.

# 編寫for迴圈,利用索引遍曆出每一個字元msg = ‘hello egon 666‘for x in range(len(msg)):    print(msg[x], end=‘ ‘)

13.

# 編寫while迴圈,利用索引遍曆出每一個字元 msg = ‘hello egon 666‘ count = 0 while count < len(msg):     print(msg[count])     count += 1

14.

# msg=‘hello alex‘中的alex替換成SB msg=‘hello alex‘ print(msg.replace(‘alex‘,‘SB‘))

15.

# msg=‘/etc/a.txt|365|get‘ 將該字元的檔案名稱,檔案大小,操作方法切割出來 msg=‘/etc/a.txt|365|get‘ print(msg.split(‘|‘))

16.

# 編寫while迴圈,要求使用者輸入命令,如果命令為空白,則繼續輸入 while True:     user=input(‘請輸入指令:‘)     if user==‘‘:         continue     else:         break

17.

# 6.編寫while迴圈,讓使用者輸入使用者名稱和密碼,如果使用者為空白或者數字,則重新輸入 while True:     name=input(‘please enter your name:‘)     passwd=input(‘please your password:‘)     if name==‘‘ or name.isdigit():            #isdigit()字串為整數         print(‘使用者名稱不可為空或者數字‘)         continue     else:         break

18.

# 7.編寫while迴圈,讓使用者輸入內容,判斷輸入的內容以alex開頭的,則將該字串加上_SB結尾 while True:                #第一種方法     con=input(‘請輸入內容:‘)     aa=‘alex‘     if con[:4]==aa[:4]:               #[:4] 切片,從開頭4位字串切片         print(con,‘_SB‘) while True:                #第二種方法     msg=input(‘請輸入內容:‘)     if msg.startswith(‘alex‘):            #startswith() 以什麼字元或字串開頭         print(msg,‘_SB‘)

19.

# 8.1.兩層while迴圈,外層的while迴圈,讓使用者輸入使用者名稱、密碼、工作了幾個月、每月的工資(整數),使用者名稱或密碼為空白,或者工作的月數不為整數,或者# 月工資不為整數,則重新輸入tag = Truewhile tag:    user = input(‘使用者名稱:‘)    passwd = input(‘密碼:‘)    work_mons = input(‘月份:‘)    salary = input(‘工資:‘)    if work_mons.isdigit() and salary.isdigit():  # isdigit()判斷是否為整數        work_mons = int(work_mons)        salary = int(salary)        # if user==‘‘ or passwd==‘‘ or not isinstance(work_mons,int) or not isinstance(salary,int):        # print(‘格式錯誤‘)    if user != ‘‘ and passwd != ‘‘ and isinstance(work_mons, int) and isinstance(salary, int):        print(‘-------------------登入成功-----------------‘)        print(‘-------歡迎進入後台查詢系統version:1.0-------‘)        print(‘----------------1.查詢總工資----------------‘)        print(‘---------------2.查詢使用者身份---------------‘)        print(‘-----------------3.退出登入-----------------‘)        print()        print()        while tag:            cmd = input(‘===>>:‘)            if cmd == ‘1‘:                print(‘總工資:‘, work_mons * salary)            elif cmd == ‘2‘:                if user == ‘alex‘:                    print(‘super suer‘)                elif user == ‘yuanhao‘ or user == ‘wupeiqi‘:                    print(‘normal user‘)                else:                    print(‘unkown user‘)            elif cmd == ‘3‘:                tag = False            else:                print(‘輸入錯誤‘)    else:        print(‘格式錯誤‘)        continue

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.