1.1 input函数的工作原理
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,将其存储在一个变量中。
message = input("Tell me something, and I will repeat it back to you:")
print(message)
#当我们使用input时,都应指定清晰而易于明白的提示
message = input("Please enter your name:")
print(message)
1.1.1 使用int()来获取数值输入
使用input函数是会将输入的信息认为是字符串信息,因此当你输入数字的时候需要将其转换成整形类型
#使用int()获取数值输入
age = input("how old are you?")#这里的age是字符串类型,需要将其转换成
age = int(age)#把str转换成int()型
print(age >= 18)
height = input("How tall are you,in inches")
height = int(height)
if height >=36:
print("\nYou are tall enough to ride!")
else:
print("\nYou'll be able to ride when you are a little older")
1.1.2 求模运算符
处理信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数
#求模运算符:会指出余数是多少
print(4 % 3)-----1
print(5 % 3)-----2
print(6 % 3)-----0
print(7 % 3)-----1
1.2 while循环
1.2.1 使用while循环
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
1.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)
#美中不足就是上述的程序在结束的时候还会把quit给打出来
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)
if message != 'quit':
print(message)
1.2.3 使用标志
#使用标志:在复杂的事件中很多不同的事件都会导致事件的结束例如,在游戏中玩家的飞船一个都没有了或者保护的城市被摧毁了
prompt = "\nTell me something, and I will repeat it back to you:"
prompt +="\nEnter 'quit' to end the program."
active = True
while active:#先循环再判断
message=input(prompt)
if message == 'quit':
active=False
else:
print(message)
1.2.4 使用break退出循环
break语句用于控制程序流程,可以用它来控制那些代码将执行,那些代码行不执行,从而让程序按你要求执行
#使用break退出循环,要立刻退出while循环,不在运行循环中余下的代码,也不管条件测试的结果如何,可以使用break语句,
#break 语句用于控制程序流程,可以用它控制那些代码执行那些代码不执行
#在任何python循环中都可以使用break语句。可以用break语句来推出遍历列表或是字典
prompt = "\nPlease enter thr 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(city)
1.2.5使用continue跳出循环
continue语句是跳出本次循环
#在循环中使用continue,是跳出本次循环
current_number = 0
while current_number < 10:
current_number+=1
if current_number % 2 ==0:
continue
print(current_number)
1.2.6 避免无限循环
#避免无限循环
#如果不小心无无限循环,那么可以使用Ctrl+C关闭显示程序输出的终端窗口
# while 1:
# print("hello world")
1.3使用循环来处理列表和字典
通过while循环,可以将while同列表和字典结合起来使用,可手机,存储并组织大量输入,共以后查看和显示
1.3.1 在列表之间移动元素
#在列表之间移动元素
unconfirmed_users = ['alice','brain','candace']#待验证用户列表
confirmed_users = []#用于存储已验证用户列表
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user: "+current_user.title())
confirmed_users.append(current_user)
print("\nThe following suers have been confirmed: ")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
1.3.2 删除包含特定值的所有列表元素
#删除包含特定值的所有列表元素
pets =['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
1.3.3 使用用户输入来填充字符
#可以使用用户输入来填充字典
#可以使用while循环提示用户输入任意数量的信息
answers ={}
poll_active = True
while poll_active:
name=input("请输入你的名字:")
answer=input("你想要在周末爬什么山:")
answers[name]=answer
repeat=input("你们是否要接着回答(yes or no)")
if repeat == 'no':
poll_active = False
print("\n--- Poll_Results---")
for name,answer in answers.items():
print(name+"想要在周末去"+answers[name])