if判断的流程控制
if语句可以根据用户的输入内容或获取的数据进行条件判断,以响应对应条件的代码块。
语法:if...else...
例子:
username = input("username:")
password = input("password:")
if username == "george" and password == "georgeNB": print("Welcome My Lord") else: print('Wrong username or password!')
语法:if...elif...else 用于多条件判断。
例子一:
age = 56 while True: user_age = int(input('input your age:')) if user_age > age: print('try smaller') elif user_age < age: print('try big') else: user_age = "exit" print('your got it!') break
例子二:
while True: score = int(input("input your score:")) if score == 100: print("your score 'A++'") elif 90 <= score <=95: print("your score 'A+' ") elif 80 <= score <= 89: print("your score 'A'") elif 70 <= score <=79: print("your score 'B+'") elif 60 <= score <=69: print("your score 'B'") elif score <=59: print("your score 'C'") else: score > 100 print("Wrong data!") break
# # # 作业 # # #
# 1、猜年龄,猜三次
age = 56 count = 0 while True: user_age = int(input('input your age:')) if user_age > age: count += 1 print(count) print('try smaller') if count >2: break elif user_age < age: count += 1 print(count) print('try big') if count >2: break else: user_age = age print('your got it!') break
# 2、猜年龄,每隔三次问一次,是否继续(Y/N)
age = 56 count = 0 while True: user_age = int(input('input your age:')) if user_age > age: count += 1 print(count) print('try smaller') if count >2: print("Do you want to choose to try again(Y/N)") choose = input("input your choose:") if choose == "Y" or "y": count =0 else: break elif user_age < age: count += 1 print(count) print('try big') if count >2: print("Do you want to choose to try again(Y/N)") choose = input("input your choose:") if choose == "Y" or "y": count = 0 else: break else: user_age = age print('your got it!') break
# 3、三次登录(残缺版1.0beta)
username = "george" password = "123" count =0 while True: input_username = input("input your username:") input_password = input("input your password:") if username == input_username and password == input_password: print("Welconme My Lord!") break else: count +=1 print("Your username or password is Wrong!") if count>2: print("Your username or passwrod is Wrong three times, It is lock") break
-------- END --------