python中的if while for語句用法


一、if判斷

       模擬人對某些事物的判斷並作出不同的決策的能力。計算機由於要像人一樣的去工作,那么它必須也要具備判斷事物對錯的能力,從而作出不同的響應。實際中的例子,面前走過一個妹紙,你會想好不好看,要不要超過去看看正臉等等。程序中比如ATM取款機,需要接收你輸入的用戶名和密碼來判斷你是否是合法用戶等等。

語法: if 條件:

代碼1

代碼2

代碼3

...

    else:

        代碼1

        代碼2

        代碼3

        ...

if...else...表示代碼成立會怎么樣,else表示不成立會怎么樣。

cls = 'human'
gender = 'female'
age = 18
if cls == 'human'and 'gender' == 'female' and age >16 and age < 22:
    print('上去表白')
elseprint(‘打擾了’')

if...elif...

if 條件1:
代碼1
代碼2
代碼3
...
elif 條件2:
代碼1
代碼2
代碼3
...
elif 條件3:
代碼1
代碼2
代碼3
...
...
else:
代碼1
代碼2
代碼3
...

if...elif...else表示if條件1成立干什么,elif條件2成立干什么,elif條件3成立干什么,elif...否則干什么。

if...elif...else

cls = 'human'
gender = 'female'
age = 28

if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('開始表白')
elif cls == 'human' and gender == 'female' and age > 22 and age < 30:
print('考慮下')
else:
print('阿姨好')

應用實例:#模擬注冊登錄

user_name = 'zzj'

user_pwd = '123'

inp_name = input('please input your name>>>:')

inp_pwd = input('please input your password>>>:')

if user_name == inp_name and userpwd == inp_pwd:

   print('登陸成功!')

else :

   print('登錄錯誤!')

#一周安排

print('''必須輸入其中一種:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday Sunday
''')
today=input('>>: ')
if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
print('上班')
elif today == 'Saturday' or today == 'Sunday':
print('出去浪')
else:
print('''必須輸入其中一種:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')

if 的嵌套

cls = 'human'
gender = 'female'
age = 18
is_success = False

if cls == 'human' and gender == 'female' and age > 16 and age < 22:
       print('開始表白')
       if is_success:
          print('那我們一起走吧...')
       else:
          print('我逗你玩呢')
else:
print('阿姨好')

二、while循環

  實現ATM的輸入密碼重新輸入的功能

  user_db = 'nick'

  pwd_db = ‘123’

  while True:

         inp_user = input('username:')

         inp_pwd = input('password:')

         if inp_user == user_db and pwd_db == inp_pwd:

            print('login successful')

         else:

            print('username or password error')

while + break

break 的意思就是終止掉當前層的循環,執行其他代碼。

#break 語句演示

while True :

        print('1')

        print('2')

        print('3')

        break

例:

user_db = 'jason'

pwd_db = '123'

while True:

   inp_user = input('username: ')

   inp_pwd = input('password: ')

     if inp_user == user_db and pwd_db == inp_pwd:

      print('login successful')

      break

     else:

    print('username or password error')

print('退出了while循環')

#while + else

  n = 1

  while n < 3:

        if n == 2:

        print(n)

        n +=1

else:

       ptint('else會在while沒有break時才會執行else中的代碼')

三、for 循環

     1. for i in range(1,10):  #range顧頭不顧尾

            print(i)

     2.name_list = ['jason','zzj','angela']

        for i in range(len(name_list)):

            prtint(i,name_list[i])

     3.for + break 跳出本層循環

        name = ['jason','zzj','angela']

        for i in name:

             if i == 'zzj':

               break

               print(i)

    4.for + continue

        name = ['jason','zzj','angela']

        for i in name:

             if i == 'zzj':

                continue

             print(i)

   5.for循環練習題

      打印九九乘法口訣表

      

for i in range(1,10):
     for j in range(1,i+1):
        print('%s*%s=%s'%(i,j,i*j),end=' ')
     print()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM