流程控制-分支语句-参考答案

作业一

写一个用户登录认证的程序,比如用户名是“Albert”,密码是“1”,请用户分别输入用户名和密码来认证,如果用户输入正确,则提示登陆成功,否则提示用户名或者密码错误。

username = 'Albert'
password = '1'

username_input = input('please input your username>>:')
password_input = input('please input your password>>:')
if username == username_input and password == password_input:
    print('Login successfully')
else:
    print('username or password is invalid')

解题思路:

image.png

作业二

写一个用户猜年龄的游戏,提前定义好用户的年龄,如果用户猜对大了,就给一个猜大了的提示,如果用户猜小了,就给一个猜小了的提示,否则就是用户猜对了,也给一个猜对的提示。

age = 18
age_input = input('please input your age>>:')
age_input = int(age_input)
if age_input > age:
    print('too old')
elif age_input < age:
    print('too young')
else:
    print("you've got it")

解题思路:

image.png