函数input()

函数input()使程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其赋给一个变量

1
2
message=input("tell me something and i will repeat:")
print(message)

实际上上面那个表达式中input里面的“”以及里面的东西不加也是可以的0_o

int()

int 只能用来将input输入的数转换成数字

1
2
3
4
5
6
from builtins import int, print

age = int(input('age='))
print(age)
result=bool(age >= 18)
print(result)
1
2
3
age=19				//19是我输入的
19
True
求模运算符

%,不解释

while循环

形式和C的差不多,但就是不用()以及:的事情

使用标签
1
2
3
4
5
6
7
8
9
10
from builtins import int, print

prompt = "\nabaxiba好吧其实我可以将你输入的话输出除非你输入quit\n"
activity = True
while activity:
message = input(prompt)
if message == 'quit':
activity = False
else:
print(message)

break中断当前循环

continue是跳出当前循环

转移列表元素
1
2
3
4
5
6
7
8
9
10
11
12
from builtins import print

unconfired_users = ['alice','bobo','ctf']#定义一个列表
confirmed_users = []#定义一个空列表
while unconfired_users:#while循环
current_user = unconfired_users.pop()#定义一个变量使之等于unconfired_users最后删除的值
print(f"verifying user:{current_user.title()}")#输出current_user的值
confirmed_users.append(current_user)#将current_user的值添加到空列表中

print('\nthe following users have been confirmed')#输出一个标题
for confirmed_user in confirmed_users:#遍历confirmed_users这个列表
print(confirmed_user)
1
2
3
4
5
6
7
8
verifying user:Ctf
verifying user:Bobo
verifying user:Alice

the following users have been confirmed
ctf
bobo
alice

这样就讲一个列表中的元素全部转移到另一个列表中了

删除特定值的列表元素
1
2
3
4
5
6
pets = ['cat','dog','lion','tiger','dargon','goldfish','fish']
print(pets)

while 'goldfish' in pets:
pets.remove('goldfish')
print(pets)
1
2
['cat', 'dog', 'lion', 'tiger', 'dargon', 'goldfish', 'fish']
['cat', 'dog', 'lion', 'tiger', 'dargon', 'fish']
这里有一个问题
1
2
3
4
5
6
pets = ['cat','dog','lion','tiger','dargon','goldfish','fish']
print(pets)
pet = ['fish','goldfish']
for pet in pets:
pets.remove(pet)
print(pets)
使用用户输入填充字典中的元素
1
2
3
4
5
6
7
8
9
10
11
12
response = {}
polling_active = True
while polling_active:
name = input("姓名:")
responses = input("回答:")
response[name] = responses
repeat = input('whether you want to answer more?yes/no')
if repeat == 'no':
polling_active = False
print("----Polling-----")
for name,responses in response.items():
print(f"{name}:回答是{response}")
1
2
3
4
5
姓名:a
回答:s
whether you want to answer more?yes/nono
----Polling-----
a:回答是{'a': 's'}