1.input函数
例子1:
name=input("please enter your name:")
print("hello,"+name)
输出结果:
please enter your name:apple
hello,apple
例子2:直接在某个字符串后输入
prompt="I am glad to meet you."
prompt+="\nHi,I am glad to meet you,"
word=input(prompt)
print("hello,"+word)
输出结果:
I am glad to meet you.
Hi,I am glad to meet you,jack
hello,jack
2.当使用input函数时,如果我们输入的是数字,系统会把这个数字当成字符,而不能用于数值型的相关操作,这时我们需要使用int(),将输入看成是数值型变量
例子:
age=input("how old are you:")
age=int(age)
age>=18
输出结果:
True
3. 求模运算符%
4%3
计算结果:1
7%3
计算结果:1
运算符最后得到的值是整除后得到的余数
4.while循环
number=1
while number<=5:
print(number)
number+=1
输出结果:
1
2
3
4
5
5.用户选择何时退出
prompt="enter your message:"
message=""
while message!='quit':
message=input(prompt)
print(message)
输出结果:
enter your message:hello
hello
enter your message:lllll
lllll
enter your message:quit
quit
6. while循环中的break和continue
break直接退出,continue是继续执行但是会返回到循环的开头
例子:
while True:
city=input()
if city == "quit":
break
else:
print(city)
输出结果:
dg
dg
quit
例子number=0
while number<10:
number+=1
if number%2==0:
continue
else:
print(number)
输出结果:
1
3
5
7
9
7.使用用户输入来填充字典
responses={}
active=True
while active:
name=input("name ")
response=input("mountains ")
responses[name]=response
repeat=input("again:")
if repeat=="no":
active=False
print(responses)
for name,response in responses.items():
print(name+" "+response)
输出结果:
name ad
mountains sfd
again:y
name hshd
mountains ahf
again:no
{'ad': 'sfd', 'hshd': 'ahf'}
ad sfd
hshd ahf
总结:
while break continue
字典的相关用法
添加字典内容,等(第六章)