ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python的while以及input()教学

2020-01-27 11:35:52  阅读:348  来源: 互联网

标签:prompt name python number int while print input


#函数input()的工作原理
message=input("Tell me something ,and I will repeat it ")
print(message)

#编写清晰的程序
name=input("please enter ypur name : ")
print("hello, "+name+"!")
prompt="If you tell us "
prompt+="\nwhat's your name ?"
name=input(prompt)# 输入的值赋值给name
print("\nhello, "+name+"!")

#使用int()来获取数值输入
height=input("how tall are you ?")
heigt=int(height)
if height>=36:
    print("you are allowed")
else:
    print("you are not allowed")

#求模运算符
number=input("Enter a number ,and I'will tell you if it's even or odd: ")
number=int(number)
if number%2==0:
    print("\nThe number "+str(number)+" is even")
else:
    print("\nThe number "+str(number)+" is odd")

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

#让用户选择何时退出
prompt="\nTell me something "
prompt+="\nenter quit to end the program."
active =True
while active:
    message=input(prompt)
    if message =='quit':
        active=False
    else:
        print(message)

#使用break退出循环
prompt="\nTell me something "
prompt+="\nenter quit to end the program."
while True:
    city=input(prompt)
    if city=='quit':
        break
    else:
        print("I'd love to go to "+city.title())

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

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

#删除包含特定值的所有列表元素——remove()
pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat'in pets:
    pets.remove('cat')
print(pets)

#使用用户输入来填充字典
response={}
polling_active=True
while polling_active:
    name=input("\nwhat's your name? ")
    answer=input("which mountain would you like to climb")
    response[name]=answer
    repeat=input("would you like to let another person respond (yes/no)")
    if repeat=='no':
        polling_active=False
print("\n ---poll results-----")
for a,b in response.items():
    print(a+" would you like to climb "+b+".")



Corey770 发布了11 篇原创文章 · 获赞 0 · 访问量 370 私信 关注

标签:prompt,name,python,number,int,while,print,input
来源: https://blog.csdn.net/qq_45662882/article/details/104091595

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有