Python 中的控制流

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Bruce_yin/article/details/77506607
    #1. 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.


#2 for statement

for i in range(1, 10):
    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 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')


4..

#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

        # 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 & 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
         # 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:
         continue
     print(i)
     
print("using pass:")    
for i in a_list:
     if not i:
         pass
     print(i) 

猜你喜欢

转载自blog.csdn.net/Bruce_yin/article/details/77506607
今日推荐