流程控制语句练习

Posted wangning110

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了流程控制语句练习相关的知识,希望对你有一定的参考价值。

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!")

 






以上是关于流程控制语句练习的主要内容,如果未能解决你的问题,请参考以下文章

流程控制语句

PHP中的流程控制

JavaScript流程控制(顺序分支循环)及相关练习

大数据技术之_23_Python核心基础学习_02_ 流程控制语句 + 序列(10.5小时)

从零开始学大数据-Java基础-流程控制语句

[第三节] Python流程控制语句 - if, while, for, break, continue