7.1 函数input()
:接受一个参数(向用户显示的提示 或说明)
在提示的末尾,包含一个空格,与用户输入分开,
如,
name = input('plz type your name: ')#冒号后,有空格
+=
运算符:在字符串后加一个字符串,(先加再赋值)
如,
say_hi = input("today")
say_hi += input("how are u?")
#say_hi = say_hi + 1
函数int()
:仅获取数值
如,
>>> age = input('how old are u?')
how old are u?1
>>> age = int(age)
>>> age > 1
False
%
求模运算符:两数相除并返回余数。(只指出余数是多少)
如,
>>> 4 % 3
1
>>> 3 % 2
1
>>> 2 % 2#整除情况下,余数为 0
0
/
7.2 while循环:满足条件则不断运行,不满足条件则为止。
num = 1
while num < 5:#数字1 < 5,就一直+1,直到 num > 5,循环结束,
print(num)
num += 1
运行结果:
1
2
3
4
/
让用户决定何时退出?
messages = 'what do u do?'
messages += "\ttype 'q' to end: "
message = ""
while message != 'q':#当 message 不是 q,满足条件,则一直打印,当不满足条件时,则结束。
message = input(messages)
print(message)
运行结果:
what do u do? type 'q' to end: w
w
what do u do? type 'q' to end: e
e
what do u do? type 'q' to end: r
r
what do u do? type 'q' to end: q
q
上面例子,结束时 q 也一同打了出来,用 if 判断去避免,
messages = 'what do u do?'
messages += "\ttype 'q' to end: "
message = ""
while message != 'q':
message = input(messages)
if message != 'q':
print(message)
运行结果:
what do u do? type 'q' to end: w
w
what do u do? type 'q' to end: e
e
what do u do? type 'q' to end: r
r
what do u do? type 'q' to end: q
/
使用标志(flag)
,满足多条件:
如,
messages = 'what do u do?'
messages += "\ttype 'q' to end: "
active = True#变量active设置为True,最初为活动状态,
while active:
message = input(messages)
if message == 'q':
active = False
else:
print(message)
运行结果:
what do u do? type 'q' to end: w
w
what do u do? type 'q' to end: e
e
what do u do? type 'q' to end: q
/
7.3 使用break
退出循环:立即退出,不再运行余下的代码,用于控制程序流程,
如,
messages = 'what do u do?'
messages += "\ttype 'q' to end: "
while True:#以 while True 打头的循环将不断运行,直到遇见 break
message = input(messages)
if message == 'q':
break
else:
print(message)
运行结果:
what do u do? type 'q' to end: w
w
what do u do? type 'q' to end: e
e
what do u do? type 'q' to end: r
r
what do u do? type 'q' to end: q
/
7.4 在循环中使用continue
:返回循环的开头,并根据条件测试结果决定是否继续执行循环。
如,
num = 0
while num < 9:
num += 1
if num % 2 == 0:
continue
print(num)
运行结果:
1
3
5
7
9
/
避免无限循环的正确表达:
如,
num = 1
while num < 5:
print(num)
num += 1#如果遗漏,循环将无限进行,
运行结果:
1
2
3
4
/
7.5 while
循环处理列表和字典:可在遍历列表的同时,对其进行需改,
unroom = ['aa','bb','cc','dd',]
inrooms = []
while unroom:
now = unroom.pop()#使用方法pop()将列表unroom末尾元素弹出,并赋值给变量 now(开始捋一捋没进房间的人名单,)
print(f"verifying:{now.title()}")#(捋清楚了,并打印出来,)
inrooms.append(now)#房间门打开,开始接受
print('\nthe following verifying:')
for inroom in inrooms:
print(inroom)
运行结果:
verifying:Dd
verifying:Cc
verifying:Bb
verifying:Aa
the following verifying:
dd
cc
bb
aa
/
使用while
删除列表的所有特定值元素:
如,
nums = [1,3,5,7,9,1,1,1,]
print(nums)
while 1 in nums:
nums.remove(1)
print(nums)
运行结果:
[1, 3, 5, 7, 9, 1, 1, 1]
[3, 5, 7, 9]
/
使用while
来填充用户输入到字典:
peoples = {}#创建一个空字典,
poll_active = True#设置一个标志,检测是否继续,
while poll_active:
name = input("what's your name: ")#收集键
ages = input("How old are u: ")#收集值
ages = int(ages)
peoples[name] = ages#添加键值对信息到字典peoples 中,
repeat = input("would u like to another person respond? (y / n)")#重复这句,问还需要其他人参加调查嘛
if repeat == 'n':
poll_active = False
print("--- polling results ---")
for name, ages in peoples.items():
print(f"My name is {name.title()}, {ages} years old.")
运行结果:
what's your name: qq
How old are u: 18
would u like to another person respond? (y / n)y
--- polling results ---
My name is Qq, 18 years old.
what's your name: xx
How old are u: 21
would u like to another person respond? (y / n)n
--- polling results ---
My name is Qq, 18 years old.
My name is Xx, 21 years old.