Python学习之路3——Python用户交互及逻辑控制

 1、Python用户交互

程序难免会与用户产生交互。

举个例子,你会希望获取用户的输入内容,并向用户打印出一些返回的结果。我们可以分别通过 input() 函数与 print()函数来实现这一需求。

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 name = input("name:")
 6 age = int(input("age:"))
 7 job = input("job:")
 8 salary = int(input("salary:"))
 9 
10 info =  """
11 --------info of {0}-------
12 name:{0}
13 age:{1}
14 job:{2}
15 salary:{3}
16       """.format(name,age,job,salary)
17 
18 print(info)
19 
20 
21 # name:VisonWong
22 # age:27
23 # job:code farmer
24 # salary:10000
25 # 
26 # --------info of VisonWong-------
27 # name:VisonWong
28 # age:27
29 # job:code farmer
30 # salary:10000

注:需要注意的是因为年龄与薪金都是数字,所以强制转化为整形。

2、Python逻辑控制

if语句: 

Python 编程中 if 语句用于控制程序的执行,基本形式为:

1 if 判断条件:
2     执行语句……
3 else4     执行语句……

其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。 

else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 
 6 name = input('请输入用户名:')
 7 pwd = input('请输入密码:')
 8 
 9 if name == "VisonWong" and pwd == "cmd":
10     print("欢迎,Vison!")
11 else:
12     print("用户名和密码错误!")
13     
14     
15 # 请输入用户名:VisonWong
16 # 请输入密码:cmd
17 # 欢迎,Vison!

当判断条件为多个值时,可以使用以下形式:

1 if 判断条件1:
2     执行语句1……
3 elif 判断条件2:
4     执行语句2……
5 elif 判断条件3:
6     执行语句3……
7 else:
8     执行语句4…… 

实例如下:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 age = 27
 6 
 7 user_input = int(input("input your guess num:"))
 8 
 9 if user_input == age:
10     print("Congratulations, you got it !")
11 elif user_input < age:
12     print("Oops,think bigger!")
13 else:
14     print("Oops,think smaller!")
15     
16     
17 # input your guess num:25
18 # Oops,think bigger!

for语句: 

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。 

for循环的语法格式如下:

1 for iterating_var in sequence:
2    statements(s) 

实例1:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 for letter in 'Python':  # 第一个实例
 6     print('当前字母 :', letter)
 7 
 8 fruits = ['banana', 'apple', 'mango']
 9 for fruit in fruits:  # 第二个实例
10     print('当前水果 :', fruit)
11 
12 print("Good bye!")
13 
14 
15 # 当前字母 : P
16 # 当前字母 : y
17 # 当前字母 : t
18 # 当前字母 : h
19 # 当前字母 : o
20 # 当前字母 : n
21 # 当前水果 : banana
22 # 当前水果 : apple
23 # 当前水果 : mango
24 # Good bye!

实例2:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 fruits = ['banana', 'apple', 'mango']
 6 for index in range(len(fruits)):
 7     print('当前水果 :', fruits[index])
 8 
 9 print("Have fun!")
10 
11 
12 # 当前水果 : banana
13 # 当前水果 : apple
14 # 当前水果 : mango
15 # Have fun!

while语句:

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

1 while 判断条件:
2     执行语句……

执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。

当判断条件假false时,循环结束。

实例:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 count = 0
 6 while (count < 9):
 7     print('The count is:', count)
 8     count += 1
 9 
10 print("Have fun!")
11 
12 
13 # The count is: 0
14 # The count is: 1
15 # The count is: 2
16 # The count is: 3
17 # The count is: 4
18 # The count is: 5
19 # The count is: 6
20 # The count is: 7
21 # The count is: 8
22 # Have fun!

while 语句时还有另外两个重要的命令 continue,break 来跳过循环。

continue 用于跳过该次循环,break 则是用于退出循环。

continue实例:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 i = 0
 6 while i <10:
 7     i += 1
 8     if i < 5:
 9         continue  # 不往下走了,直接进入下一次loop
10     print("loop:", i)
11 
12 
13 # loop: 5
14 # loop: 6
15 # loop: 7
16 # loop: 8
17 # loop: 9
18 # loop: 10

break实例:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 i = 0
 6 while i <10:
 7     i += 1
 8     if i > 5:
 9         break  # 不往下走了,直接跳出整个loop
10     print("loop:", i)
11     
12 
13 # loop: 1
14 # loop: 2
15 # loop: 3
16 # loop: 4
17 # loop: 5
原文地址:https://www.cnblogs.com/visonwong/p/8613209.html