python编程 从入门到实践 第七章 用户输入和while 循环

学习笔记:

# 用户输入和while循环
##
# 7.1 函数input()的工作原理
message=input("tell me something ,and i will repeat it back to you! ")
print(message)
## imput()接受一个参数,运行第一行,显示提示信息,等待输入,,回车输入,输入存储在变量message里,最后进行print()

# 7.1.1 编写清晰的程序
name=input("please enter your name: ")
print("hello, "+name+" !")
## 最好指明清晰明白的提示
prompt="if you tell us who you are ,we can personalize the messages you see. "
prompt+="\n what is your first name? "      ## +=是在字符串的尾端加上字符串
name=input(prompt)
print("hello, "+name+" !")
## 提示超过一行,可以先将提示存储在一个变量里,再将变量传递给input()
#7.1.2 使用Int()来获取数数值输入
## 前提: 因为函数input()将用户输入理解为字符串,所以数字不可以当做数字使用
age=input("how old are you? ")
print(age)

#7.2 s使用Int()来获取数值输入
height=input("how tall are you ?")
height=int(height)
if height>=36:
    print("you are tall enough to ride ")
else:
    print("you can't ride!")

#7.1.3 求模运算符
number=input("please enter a number: ")
number=int(number)
if number % 2 == 0:
    print("\n the number "+str(number)+" is even. ")
else:
    print("\n the number "+str(+number)+" is odd. ")

# python2.7 中获取输入
# raw_input()来提示输入  同样解读为字符串

#7.2 while循环简介
# 7.2.1 使用while循环
current_number=1
while current_number<=5:
    print(current_number)
    current_number+=1

#7.2.2 让用户选择何时退出
prompt="\n tell me something!! "
prompt+="\n enter quit to end.."
message=""
while message != 'quit':
    message=input(prompt)
    if message != 'quit':
        print(message)
##首次遇到这个循环时,message是一个空串,因此进入循环,

#7.2.3 使用标志
prompt="\n tell me something!! "
prompt+="\n enter quit to end.."
active = True
while active:
    message=input(prompt)
    if message=='quit':
        active=False
    else:
        print(message)
## 添加一个标志,用于判断是否应该继续运行

# 7.2.4 使用break退出循环
prompt="\n tell me something!! "
prompt+="\n enter quit to end.."
while True:
    message=input(prompt)
    if message =='quit':
        break
    else:
        print("i would like to go "+message.title())
## 任何循环都可以使用break,

#7.2.5 在循环中使用continue
current_number=0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    print(current_number)

# 7.2.6 避免无限循环
x=1
while x<=5:
    print(x)
    x+=1

#7.3 使用循环while 来处理列表和字典
## for循环遍历列表有效,但是不应修改列表

# 7.3.1 在列表之间移动元素
unconfirmed_users=['alice','brain','candace']
confirmed_users=[]
while unconfirmed_users:
    current_user=unconfirmed_users.pop()
    print("verifying user: "+current_user.title())
    confirmed_users.append(current_user)
print("\n the following users have been confirmed: ")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

#7.3.2 删除包含特定值的所有列表元素
pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print(pets)
## 当出现多个cat在列表中时,当至少出现一次时,进入循环,删除一个,再回到循环,再进行删除,最后退出循环

#7.3.3 使用用户输入来填充字典
responses={}
polling_active=True #设置一个标志,指出调查是否继续
while polling_active:
    name=input("\nwhat is your name? ")
    response=input("\nwhich mountain would you like to climb? ")
    responses[name]=response
    ##将答案存储在字典中
    repeat=input("would you like to let another person respond? (yes/no)")
    if repeat=='no':        #看看是否还有人要参加调查
        polling_active=False
    print("\n-----poll result------")
    for name,response in responses.items():
        print(name+"would you like to climb "+response+' . ')
## 建立一个标志,polling_active是否要进行循环

GOOD LUCK!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值