1.变量引用练习
1 #-*-coding:utf-8-*- 2 # Author wangning 3 name = input("Name:") 4 age = input("Age:") 5 job = input("Job:") 6 salary = input("Salary:") 7 8 info = """ 9 -----------------------information %s ------------------------------- 10 Name: %s 11 Age: %s 12 Job: %s 13 Salary: %s 14 """ %(name,name,age,job,salary) 15 16 info2 = """ 17 ------------------------information {_name} -------------------------------- 18 Name:{_name} 19 Age:{_age} 20 Job:{_job} 21 Salary:{_salary} 22 """ .format(_name=name,_age=age,_job=job,_salary=salary) 23 24 info3 = """ 25 --------------------------infomation {0} ----------------------------------- 26 Name:{0} 27 Age:{1} 28 Job:{2} 29 Salary:{3} 30 """ .format(name,age,job,salary) 31 32 #依次打印info info2 info3 33 print(info3)
2.if语句练习
1 #-*-coding:utf-8-*- 2 #Author wangning 3 _name = "wangning" 4 _password = "abc123" 5 username = input("username:") 6 password = input("password:") 7 if username == _name and password == _password: 8 print("Welcome user {name} login...".format(name=username)) 9 else: 10 print("invalid username and password")
3.while循环猜年龄
练习1
1 #-*-coding:utf-8-*- 2 # Author wangning 3 age_of_wangning = 28 4 count = 0 5 while count < 3: 6 age = int(input("please guess age:")) 7 if age == age_of_wangning: 8 print("congratuation you,you have guess it!") 9 break 10 if age > age_of_wangning: 11 print("the number is too big") 12 else: 13 print("the number is too small") 14 count += 1 15 else: 16 print("you have tried too many time")
练习2
1 #-*-coding:utf-8-*- 2 # Author wangning 3 age_of_wangning = 28 4 count = 0 5 while count < 3: 6 age = int(input("please guess age:")) 7 if age == age_of_wangning: 8 print("congratuation you,you have guess it!") 9 break 10 if age > age_of_wangning: 11 print("the number is too big") 12 else: 13 print("the number is too small") 14 count += 1 15 else: 16 print("you have tried too many time")
4.for循环猜年龄
1 #-*-coding:utf-8-*- 2 # Author wangning 3 age_of_wangning = 28 4 for n in range(3): 5 age = int(input("please guess age:")) 6 if age == age_of_wangning: 7 print("congratuation you!") 8 break 9 elif age > age_of_wangning: 10 print("the number is too small") 11 else: 12 print("the number is too big") 13 else: 14 print("Enter the number too much!")