用户输入和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")
2.使用while循环
num = 1
while num <=5:
print(num)
num+=1
(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)
(3)循环中使用continue
num = 0
while num <= 10:
num +=1**
if num % 2 == 0:
continue #满足条件继续循环,不满足跳出**
print(num) **#不写在循环内**
number = 1
while number >3:
print(number)
(4)#删除包含特定值的所有列表元素
pet = ["dog","dog","dog","cat","rabbit"]
print(pet)
while "dog" in pet:
pet.remove("dog")
print(pet)
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 + ".")
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页名称:用户输入和while循环-创新互联
转载源于:http://pwwzsj.com/article/dcjdhp.html