python编程从入门到实践学习记录---第七章 用户输入和while循环 知识梳理

本文详细介绍了Python中的输入函数input()的使用方法,包括基本的字符串输入、字符串到数字的转换,以及如何利用while循环进行连续输入直到满足特定条件。通过实例展示了求余数运算、列表和字典的操作技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、函数 input()
提示2行输出 重点 +=
prom=“If you tell me who you are ,i can return it back to you .”
prom+="\nPlease input you name:"
name=input(prom)
#输入提示框 输入pick
print("\n Hello "+ name )

2、input()输入获取的时字符串,如果想数字字符串转换成数字需要用int () 函数。
age =input(“please input you age:”)
please input you age:20
age=int(age)
if age>18:
print("you old enough! ")
eles:
print(“you too young!”)

3、%求余数运算。

4、while循环,让用户选择退出
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
message=""
while message!=“quit”:
message=input(prom)
print(message)

下面不打印quit
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
message=""
while message!=“quit”:
message=input(prom)
if message!=“quit”:
print(message)

使用标志
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
flag=True
while flag:
message=input(prom)
if message==“quit”:
flag=False
else:
print(message)

使用break退出循环 。
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."

while True:
message=input(prom)
if message==“quit”:
break
else:
print(message)

使用continue 退出当次循环。
打印1–10中的奇数
python 中没有i++ ,i–,用i=i+1 或者 i+=1

cur_num=0
while cur_num<10:
    cur_num+=1
    if cur_num%2==0:
        continue
    print(cur_num)

5、使用while处理列表和字典
在列表中移动元素

un_user=["tom","rose","bike"]
config_user=[]
while un_user:
    user=un_user.pop()
    config_user.append(user.title())
for co in config_user:
    print(co.title())

删掉包含特定值的所有列表元素

citys=["beijjing","shanghai","najing","shanghai","hefei","shanghai"]
# ci="shanghai"
while "shanghai" in citys:
    citys.remove("shanghai")
print(citys)

while 填充字典 姓名和爱好对应起来,利用键-值 字典

responses={}
flag=True
while flag:
    name=input("\nwhat you name?")
    answer=input("what you like?")
    responses[name]=answer
    ag=input("do you have another response?(yes/no)")
    if ag=="no":
        flag=False
print("\n -----the result----- ")
for name,response in responses.items():
    print(name+" will like "+response+".")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值