Python_控制流语句

1.if 语句

if condition:
     do something
elif other_condition:
     do something

2.for 语句

Code:

 #if statement example

 number = 59
 guess = int(input('Enter an integer : '))

 if guess == number:
    # New block starts here
     print('Bingo! you guessed it right.')
     print('(but you do not win any prizes!)')
     # New block ends here
 elif guess < number:
     # Another block
     print('No, the number is higher than that')
     # You can do whatever you want in a block ...
 else:
     print('No, the number is a  lower than that')
     # you must have guessed > number to reach here

 print('Done')
 # This last statement is always executed,
 # after the if statement is executed.


#the for loop example

 for i in range(1, 10): #[1,9)
     print(i)
 else:
     print('The for loop is over')


 a_list = [1, 3, 5, 7, 9]
 for i in a_list:
     print(i)

 a_tuple = (1, 3, 5, 7, 9)
 for i in a_tuple:
     print(i)

 a_dict = {'Tom':'111', 'Jerry':'222', 'Cathy':'333'}
 for ele in a_dict:
     print(ele)
     print(a_dict[ele])

 for key, elem in a_dict.items():
     print(key, elem)

3.while语句

4.range语句

Code:

#while example

 number = 59
 guess_flag = False


 while guess_flag == False:
     guess = int(input('Enter an integer : '))
     if guess == number:
         # New block starts here
         guess_flag = True

         # New block ends here
     elif guess < number:
         # Another block
         print('No, the number is higher than that, keep guessing')
         # You can do whatever you want in a block ...
     else:
         print('No, the number is a  lower than that, keep guessing')
         # you must have guessed > number to reach here

 print('Bingo! you guessed it right.')
 print('(but you do not win any prizes!)') 
 print('Done')

#For example 

number = 59
num_chances = 3
print("you have only 3 chances to guess")

for i in range(1, num_chances + 1):
    print("chance " + str(i))
    guess = int(input('Enter an integer : '))
    if guess == number:
        # New block starts here
        print('Bingo! you guessed it right.')
        print('(but you do not win any prizes!)') 
        break #跳出for循环

        # New block ends here
    elif guess < number:
        # Another block
        print('No, the number is higher than that, keep guessing, you have ' + str(num_chances - i) + ' chances left')
        # You can do whatever you want in a block ...
    else:
        print('No, the number is lower than that, keep guessing, you have ' + str(num_chances - i) + ' chances left')
        # you must have guessed > number to reach here


print('Done')

5.break

6.continue

7.pass

Code:

#break & continue example
 number = 59

 while True:
     guess = int(input('Enter an integer : '))
     if guess == number:
         # New block starts here
         break

         # New block ends here
     if guess < number:
         # Another block
         print('No, the number is higher than that, keep guessing')
         continue #直接跳到while进入下一次循环
         # You can do whatever you want in a block ...
     else:
         print('No, the number is a  lower than that, keep guessing')
         continue #可有可无
         # you must have guessed > number to reach here

 print('Bingo! you guessed it right.')
 print('(but you do not win any prizes!)') 
 print('Done')

continue and pass difference

 a_list = [0, 1, 2]

 print("using continue:")
 for i in a_list:
     if not i: #not 0是真,not 2是假
         continue 
         #当前循环体后面语句不执行,直接跳到下一次循环
     print(i)

 print("using pass:")    
 for i in a_list:
     if not i:
         pass 
         #相当于空代码,什么也不做,后面的语句将执行
     print(i) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值