Day_02-Python branching structure and loop structure

Branch structure

Scenarios

  So far, we write Python code is a statement in the order of execution, the structure of the code that we call sequential structure. However, only the sequence structure does not solve all the problems, such as we design a game, the game first hurdle clearance condition is the player get 1000 points, then after the completion of this Council game we have to get points according to the player to decide whether to enter the second off or tell the player "Game Over", where it will have two branches, but only one of these two branches will be executed, this is the program branch structure.

Use 1.if statement

In python, to construct a branch structure can be used if, elif and else keywords.

Exercise: user authentication

Code:

username = input('user:')
password = input('pwd:')
if username == 'admin' and password == '123':
    print('login')
else:
    print('Error')
operation result:

It should be noted that the other language is different, python did not use curly braces to construct the code but the use of the advanced ways to set up a hierarchy of code, if ifyou need to perform multiple statements under the condition is satisfied, more than just keep statement with the same indentation can be, in other words, if successive code while maintaining the same indentation then they belong to the same block, the equivalent of a whole is performed.

Of course, if you want to construct a more branches, may be used if…elif…else…the structure, for example the following piecewise function evaluation.

Code:

x = int (input ( 'Enter:'))
if x > 1:
    f = 3 * x - 5
elif x < -1:
    f = x + 2
else:
    f = x * 5 + 3
print(f)
operation result:

3. Calculator

Code:

num4, num5 = map (float, input ( 'Enter two numbers:'.) split ( ','))
choose_method = input('choose method')
if choose_method == '+':
  print('%f + %f = %f'%(num4,num5,num4 + num5))
elif choose_method == '-':
  print(num4 - num5)
elif choose_method == '*':
  print(num4 * num5)
elif choose_method == '/':
  print(num4 / num5)
else:
  raise KeyError('Only choose [+,-,*,/]')

 Operational results:

4. A rock-scissors game

Code:

import numpy as np
method = input ( 'Please punches:')
computer = np.random.choice ([ 'rock', 'scissors', 'cloth'])
if method == 'stone' and computer == 'stone':
print ( '{} draw out his Yo!'. format (computer))
elif method == 'stone' and computer == 'scissors':
print ( '{he} you win out of it!'. format (computer))
elif method == 'stone' and computer == 'cloth':
print ( '{} his out you lose Yo!'. format (computer))
elif method == 'scissors' and computer == 'stone':
print ( '{} his out you lose Yo!'. format (computer))
elif method == 'scissors' and computer == 'scissors':
print ( '{} draw out his Yo!'. format (computer))
elif method == 'scissors' and computer == 'cloth':
print ( '{he} you win out of it!'. format (computer))
elif method == 'cloth' and computer == 'stone':
print ( 'him out of {} you win Yo!'. format (computer))
elif method == 'cloth' and computer == 'scissors':
print ( 'him out of {} you lose it!'. format (computer))
elif method == 'cloth' and computer == 'cloth':
print ( '{} draw out his Yo!'. format (computer))
 operation result:

5. Enter the length of the three sides of the triangle is determined whether, if the calculation of perimeter and area

Code:

import math
a = float (input ( 'Enter edge length 1:'))
b = float (input ( 'Enter edge length 2:'))
c = float (input ( 'Enter edge length 3:'))
if (a + b > c) and (a + c >b) and (b + c < a):
print ( 'circumference:.% 1f'% (a + b + c))
p = (a+b+c)/2
area = math.sqrt(p * (p-a) * (p-b) * (p-c))
print ( 'Area:.% 1f'% area)
else:
print ( 'this is not a triangle')
 operation result:

 

Guess you like

Origin www.cnblogs.com/KAJIA1/p/11275873.html