函数input()的工作原理
函数input()可以让程序暂停,等待用户输入一些文本。
message=input('Tell me something,and I will repeat it back to you :')
print(message)
#7.1.1 编写清晰的程序
#使用input函数时,都应该指定清晰易懂的提示
name=input("Please enter your name:")
print(f"\nHello ,{name}!")
有时候提示可能超过一行,可以给提示赋给一个变量,再将该变量传递给函数input()
#提示信息超过一行
promt='If you tell us who you are , we can personalize the messages you s'
promt+="\nWhat is your first name?"
name=input(promt)
print(f"\nHello,{name}!")
#7.1.2 使用int()来获取数值输入
age=input("How old are you ?")
print(age)
我们怎么知道python将输入作为字符串还是数字呢?如果要输入数字进行比较呢?
int()函数,可以将数的字符串表示转化为数值类型
age=input("How old are you ?")
print(age)
age=int(age)
print(age>=18)
在实际程序中使用int()函数,关于身高的例子
height=input("How tall are yiou, in inches?")
height=int(height)
if height>=48:
print("\nYou`re tall enough to ride!")
else:
print("\n You`ll be able to ride when you`re a little older.")
取模运算。返回两数相除的余数
#7.1.3求模运算
#%运算是一个很有用的工具,将两个数相除并返回余数
print(4%3)
print(5%3)
print(6%3)
print(7%3)
#取模运算不会指出一个数是另一个数的多少倍,只指出余数是多少。
#利用余数判断是奇/偶数
number=input("Enter a number,and I`ll tell you if it`s even or odd:")
number=int(number)
if number%2==0:
print(f"\n The number {number} is even.")
else:
print(f"\nThe number {number} is odd.")
练习:
# 汽车租赁
print("what kind or car you like?")
car=input()
print(f"Let me see if I can find you a {car}")
# 餐馆订位
print("how many people wile coming in?")
people_numbers=input()
if int(people_numbers)>8:
print(f"the people_number is{people_numbers} and over 8,didn`t have enough table")
else:
print("there are one table to using ")
#10的整数倍
print("please input a number")
number=input()
if int(number)%10 == 0:
print("是10的倍数")
else:
print("不是10的倍数")
while循环
#for 循环用于针对集合中的每个元素都执行一个代码块,而while循环则不断运行,直到指定的条件不满足为止
#7.2.1 使用while循环
current_number=1
while current_number<=5:
print(current_number)
current_number+=11
#7.2.2 让用户选择何时退出
# prompt='\nTell me something,and I will repeat it back to you;'
# prompt += "\nEnter 'quit' to end the program."
# message = ""
# while message != 'quit':
# message = input(prompt)
# print(message)
#7.2.3 使用标记
#不同的事情导致程序停止运行
#可以定义一个变量,用于判断整个程序是否处于活动状态,这个变量称为标记,充当程序的交通信号
# prompt = "\n Tell me sometjing,and I will repaet it back to you:"
# prompt+="\n Enter 'quit' to end the program."
# active=True
# while active:
# message=input(prompt)
#
# if message=='quit':
# active=False
# else:
# print(message)
#7.2.4 使用break推出循环
# prompt="\n Please enter the name of a city you have visited"
# prompt+="\n(Enter 'quit' when you are finished.)"
#
# while True:
# city=input(prompt)
#
# if city=='quit':
# break
# else:
# print(f"I`d love to go to {city.title()}")
#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 避免无限循环
#test
print("please input something ")
while True:
flag=input()
if flag=='quit':
break
else:
print("please input again")
while循环处理列表和字典
``
#使用while处理列表和字典
#for循环水是一种有效的遍历方式,但是不应该在for循环中修改列表
#7.3.1 在列表之间移动元素
#首先,创建一个等待验证用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users=['alice','brian','candace']
confirmed_users=[]
#验证每个用户,直到没有未验证用户为止
#将每个经过验证的用户都移到已验证用户列表中
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print(f"Verifying user:{current_user.title()}")
confirmed_users.append(current_user)
#显示所有已验证的用户
print("\nThe 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)
#7.3.3 使用用户输入来填充字典
pesponses={}
#设置一个标志,指出调查是否继续
polling_active=True
while polling_active:
#提示输入被调查者的名字和回答
name=input("\n what is your name? ")
response = input("which mountain would you like to climb someday?")
#看看是否还有人要参与调查
repeat=input("would you like to let another persion respond? (yes/n)")
if repeat=='no':
polling_active=False
#调查结束,显示结果
print("\n--- Poll results --")
for name,response in response.items():
print(f"{name} would like to climb {response}.")