Python学习日记 Day7用户输入与while循环

今天是2020年2月16日,晴,0~5℃

一、用户输入:input函数

在程序运行时,往往需要用户为程序提供信息,这就需要用户输入。
input 函数将暂停程序运行,等待用户输入信息,并将该信息作为返回值。

#输出用户输入的字符串
name = input()		#Input:Alex Tu
print(name)			#Output:Alex Tu

input 函数也会读入" "等字符,但遇回车结束输入。

input函数可以接收一个参数,作为显示在程序上的提示。

#添加输入提示
name = input("What's your name?\n")		#Output:What's your name?	Input:Alex
print("Nice to meet you, " + name)		#Output:Nice to meet you, Alex

input 函数将把用户输入作为字符串返回。若想转换为数值类型,需要 int 函数或 float 函数。

#判断数据类型
number = input()		#Input:6
print(type(number))
number = int(number)
print(type(number))
'''
Output:
<class 'str'>
<class 'int'>
'''
● 求模运算符

此处补充一个运算符" % ",结果为前数除以后数的余数。用此运算符可以判断一个整数为奇数或偶数。

#判断奇偶性
number = int(input("Please Enter a Int:"))
if number % 2 == 0:
	print("It's even!")
else:
	print("It's odd!")

二、while循环

● while循环基本用法

for 循环针对集合中的元素,而 while 循环针对指定条件。当条件满足时,将不断执行循环,直到条件不满足。

'''
while conditional_test:
	do something
'''
#从1数到5
i = 1
while i <= 5:
	print(i)
	i += 1
'''
Output:
1
2
3
4
5
'''
● 选择退出

在条件中添加退出值,可以让用户选择何时退出。

#用户输入0退出
choice = input("Enter 0 to quit! ")						#Input:1
while choice != "0":
	print("Illegal Choice!")
	choice = input("Please Enter 0 to quit! ")			#Input:0
print("Thank you for your using!")
'''
Output:
Enter 0 to quit! 1
Illegal Choice!
Please Enter 0 to quit! 0
Thank you for your using!
'''
●使用标志

有时一个循环中,有多种情况可能导致循环结束,此时可以使用标志作为 while 循环的条件。

#用户输入quit,end,esc的任意大小写组合退出
flag = True
while flag:
	message = input()
	if message.lower() == "quit":
		flag = False
	elif message.lower() == "end":
		flag = False
	elif message.lower() == "esc":
		flag = False
	else:
		print(message)
print("Thanks for you using!")
● 退出循环:break关键字

当在循环内需要立刻退出循环且不执行余下语句时,可以使用 break 关键字。

#当被减数小于减数时,退出循环
while True:
    number1 = int(input())
    number2 = int(input())
    if number1 < number2:
        print("Error!")
        break
    print(number1 - number2)
print("End")
#Input1:5 , 4  Output1:1
#Input2:4 , 5  Output2:Error! , End
● 忽略余下语句:continue关键字

当在循环内需要立刻回到开头且不执行余下语句时,可以使用continue关键字。

#当被减数小于减数时,重新循环
while True:
    number1 = int(input())
    number2 = int(input())
    if number1 < number2:
        print("Error!")
        continue
    print(number1 - number2)
print("End")
#Input1:5 , 4  Output1:1
#Input2:4 , 5  Output2:Error!
#Input3:6 , 4  Output3:2

三、用while循环处理列表和字典

for 循环能有效地遍历列表,但不应修改列表,否则Python将难以跟踪列表中的元素。若想在遍历时进行修改,可以使用 while 循环。通过将 while 循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

1、在列表之间移动元素

while 循环可在列表之间搬运元素,同时进行操作。

#将社区1中的人搬家至社区2
community_1 = ["Smith" , "John" , "Parker"]
community_2 = []
while community_1:
    household = community_1.pop()
    print(household + " has moved to community_2")
    community_2.append(household)
print("All of them have moved to community_2!")
'''
Output:
Parker has moved to community_2
John has moved to community_2
Smith has moved to community_2
All of them have moved to community_2!
'''
2、删除列表中特定值的全部元素

remove 函数可以删除列表中某个特定值的元素,但只能删除第一个。如果将 remove 函数和 while 循环一起使用,可以删除全部特定值的元素。

#删除列表里所有的1
number_list = [1,1,2,3,4,1,1,5,1,6,7,1,1,1,8,1,9]
while 1 in number_list:
	number_list.remove(1)
print(number_list)		#Output:[2, 3, 4, 5, 6, 7, 8, 9]
3、用用户输入填充字典

利用while循环可以为字典填充信息。

#学生信息表
student_list = []
flag = True
while flag:
    student = {}
    student["Name"] = input("Please Enter the name: ")
    student["Age"] = input("Please Enter the age: ")
    student["Major"] = input("Please Enter the major: ")
    student_list.append(student)
    choice = input("Continue to input? ")
    if choice.lower() == 'no':
        flag = False
print("\n----------Student List-----------")
i = 1
for student in student_list:
    print("student" + str(i) + ":")
    i += 1
    for key,value in student.items():
        print(key + " : " + value)
'''
Test:
Please Enter the name: Alex
Please Enter the age: 19
Please Enter the major: CS
Continue to input? yes
Please Enter the name: Peter
Please Enter the age: 21
Please Enter the major: Acct
Continue to input? yes
Please Enter the name: Chris
Please Enter the age: 20
Please Enter the major: CE
Continue to input? no

----------Student List-----------
student1:
Name : Alex
Age : 19
Major : CS
student2:
Name : Peter
Age : 21
Major : Acct
student3:
Name : Chris
Age : 20
Major : CE
'''

钱财这个词同时就意味着奴役。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值