python基礎題

墓地行者發表於2018-08-08

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

def print_sum():
    num = 0
    count = 1
    while count< 101:
        num = num + count 
        count += 1
    print(num)

2.輸出1-100內所有奇數

def print_odd_number():
    count=1
    while count<101:
        odd_number=count%2==0
        if odd_number:
            pass#偶數不列印
        else:
            print('奇數:',count )

3.輸出1-100內所有偶數

def print_even_number():
    count=1
    while count<101:
        even_number=count%2==0
        if even_number:
            print('偶數:',count )
        else:
            pass#偶數不列印

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

def print_odd_even_sum():
    num = 0
    count = 1
    while count< 101:
        even_number=count%2==0
        if even_number:
            num = num - count
         else:
             num = num + count
        count = count + 1
    print(num)

5.使用者登入錯誤三次限制:

def login_control():
    count=0
    while count<3:
        username=input('請輸入使用者名稱:')
        password=input('請輸入密碼:')
        results =username=='123' and password=='123'
        if results:
            print('登入成功')
            break;
        else:
            print('賬號或密碼錯誤')
            count=count+1   

相關文章