1. 程式人生 > >Python學習基本小練習

Python學習基本小練習

對於python的10個小練習做下筆記

1.使用while迴圈輸入1 2 3 4 5 6 8 9 10。。。自己寫的程式碼如下:

num1 = 0
while num1 < 10:
    num1 = num1 +1
    if num1 == 7 :
        continue
    print(num1)
視訊教程:
n = 1
while n < 11:
    if n == 7:
        pass
    else:
        print(n)
    n = n +1  

 

2.求1-100的所有數的和

num2 = 1
result 
= 0 while num2 <= 100: print(num2) result = result + num2 num2 = num2+1 print(result) 視訊教程: n = 1 s = 0 while n < 101: s = s +n n = n+1 print(s)

3.輸出1-100內的所有的奇數

num3 = 1
while num3 <= 100:
    if num3 % 2 != 0:
        print(num3)
    num3 = num3 + 1
視訊教程:
NUM1 = 1
while
NUM1< 101: temp = NUM1 % 2 if temp == 0: pass else: print(NUM1) NUM1 = NUM1 +1

4.輸出1 -100 內的所有偶數

num4 = 1
while num4<=100:
    if num4 % 2 == 0:
        print(num4)
    num4 = num4 +1
視訊教程:
n = 1
while n < 101:
    temp = n % 2
    if temp ==0 :
        print(n)
    
else: pass n = n+1 print("END----------------")

5.求1-2+3-4+5...99的所有數的和

num5 = 1
result1 = 0
result2 = 0
while num5 < 100:
    if num5 % 2!=0:
        result1 = result1 + num5
    else:
        result1 = result1 - num5
    num5 =num5 +1
    print(result1)
視訊教程:
n = 1
s = 0
while n <100:
    temp = n % 2
    if temp == 0:
        s=s-n
    else:
        s = s+n
    print(s)

 6.要求使用者登入系統,只有3次機會

n = 3
n1 = 'xiaoming'
n2 = '123456'
while n > 0:
    name = input("請輸入使用者名稱:")
    Pass = input("請輸入密碼:")
    n = n-1
    if name == n1 and Pass ==n2:
        print("歡迎來到東京熱!")
        break
    elif name == n1 and Pass !=n2:
        print("您輸入的密碼錯誤,請重新輸入,您還有%d次機會"%n)
    else:
        print("請輸入正確的使用者名稱!您還有%d次機會"%n)