#函数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+".")
python的while以及input()教学
最新推荐文章于 2022-09-10 21:15:27 发布