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