1. 程式人生 > >Study 5 —— 流程控制

Study 5 —— 流程控制

one pan little lis -s put pri less 多分支

if 條件:
滿足條件後要執行的代碼
else:
if條件不滿足就執行這裏

#_*_coding:utf-8_*_
---------------------------------------------------	
#單分支
username = ‘Lisa‘
password = ‘123‘
Username = input(‘Input your name: ‘)
Password = input(‘Input your password: ‘)

if Username == username and Password == password :
print(‘Welcome to the system!‘)
else :
print(‘The username or password is not right!‘)
---------------------------------------------------
#雙分支
Name = input(‘Input your name: ‘)
Sex = input(‘Input your sex: ‘)
Age = int(input(‘Input your age: ‘))

if Sex == ‘girl‘ :
if Age < 18 :
print(‘She is a little princess!‘)
else :
print(‘She is going to marry!‘)
else :
print(‘No one likes him!‘)
---------------------------------------------------
#多分支
(1)
age = 26

user_guess = int(input(‘Input a number you guess: ‘))

if user_guess == age :
print(‘You are right!‘)
elif user_guess > age :
print(‘Try smaller!‘)
else :
print(‘Try bigger!‘)

(2)#根據成績不同,給予不同等級
Score = int(input(‘Input Score: ‘))

if Score <= 100 :
if Score >= 90 : 
print(‘A‘)
elif Score >= 80 :
print(‘B‘)
elif Score >= 60 :
print(‘C‘)
elif Score >= 40 :
print(‘D‘)
elif Score >= 0 :
print(‘E‘)
else :
print(‘You are a fool!‘)
else :
print(‘Must be less than 100!‘)

  

Study 5 —— 流程控制