Day1_Python基础_13.表达式if ... else

场景一、用户登陆验证

# Author: George.Wang
import getpass
_Username="George"
_Password="abc123"

Username=input("Please input your name:")
#Password=input("PLsease input your password:")
Password=getpass.getpass("Please input your password:") #pycharm中会有问题,python console可用

if Username==_Username and Password==_Password:
print("Hi,{name} Welcome to login this system!".format(name=Username))
else:
print("Warning!Our system have an attack!")
print("新的程序段,必须顶着开始,不得有空格或非法缩进,此例为错误")
 

场景二、猜年龄游戏

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了

my_age = 32
guess_my_age = int(input("You can guess my age:"))
if guess_my_age < my_age :
print("I'm sorry, your answer is smaller than that...let's try again.")
elif guess_my_age == my_age:
print("Congratulations! you have got the right age.")
else:
print("I'm sorry, your answer is bigger than that...let's try again.")
外层变量,可以被内层代码使用
内层变量,不应被外层代码使用
 
原文地址:https://www.cnblogs.com/wangcx/p/6701016.html