快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

用户输入和while循环

1.函数input()的工作原理

成都创新互联专业为企业提供沁源网站建设、沁源做网站、沁源网站设计、沁源网站制作等企业网站建设、网页设计与制作、沁源企业网站模板建站服务,十余年沁源做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

message = input("Tell me something,I will repeat it back to you: ") #input()函数:让程序停止运行,等待用户输入一些文本
print(message)
name = input("Please enter your name: ")
print("Hello," + name + "!")
prompt = "If you tell us who you are,we can personalize the message you see." #创建多行字符串,+=:再附加
prompt += "\nWhat is your first name?"
name_a = input(prompt)
print("Hello "+ name_a + "!")
#求模运算符
number = input("Enter a number,and I will tell you if it's even or odd: ") #使用input()
number = int(number) #用户输入的数值是字符串表示的,转化为数值
if number % 2 == 0:
print("\n The number " + str(number) + " is even.")
else:
print("\nThe number " + str(number) + " is odd.")
print("\n")

用户输入和while循环
2.使用while循环

num = 1
while num <=5:
print(num)
num+=1
用户输入和while循环

(2)#让用户选择何时退出

prompt = "\nTell me something,I will repeat it back to you."
prompt += "\nEnter 'quit' to end program. "
message = ""
while message != "quit": #message变量来提供判断
message = input(prompt) #input()都需要一个变量存储
if message != "quit": #打印前加个if条件再打印
print(message)
#使用标志
active = True #Ture:程序处于活跃状态
while active:
message = input(prompt)
if message == "quit": #用if语句判断程序状态
active = False
else:
print(message)
#使用break退出循环
while True:
message = input(prompt)
if message == "quit":
break
else:
print(message)
用户输入和while循环

(3)循环中使用continue

num = 0
while num <= 10:
num +=1**
if num % 2 == 0:
continue
#满足条件继续循环,不满足跳出**

print(num)                               **#不写在循环内**

number = 1
while number >3:
print(number)
用户输入和while循环

(4)#删除包含特定值的所有列表元素

pet = ["dog","dog","dog","cat","rabbit"]
print(pet)
while "dog" in pet:
pet.remove("dog")
print(pet)
用户输入和while循环

3.#使用用户输入来填充字典

responses = {} #创建空字典
active = True #循环状态
while active:
name = input("What is your name? " ) #变量做键
response = input("Where are you want to go? ") #变量做值
responses[name] = response #添加键-值
repeat = input("Would you like to let another person respond? (yes/no) ")
if repeat == "no": #if判断状态
active = False
print("\n---poll Result---")
for name,response in responses.items(): #items():两个变量分别存储键-值
print(name + " would like to go " + response + ".")
用户输入和while循环


文章标题:用户输入和while循环
链接URL:http://6mz.cn/article/jjesje.html

其他资讯