input()函数让程序暂停运行,等待用户输入一些文本,获取用户输入后,将用户输入的内容存储在一个变量中。
input()函数接受一个参数:即要向用户显示的提示或说明。
举个栗子
>>> message=input('please input something: ') please input something: something >>> print(message) something #可以用一系列的字符串操作可以使得我们的程序更加清晰使用函数Input(),Python 将用户的输入解读为字符串。当我们需要对数字(int float)进行操作时,我们需要用类型转化函数来讲字符串变量变成数字类型的变量。
举个栗子:
>>> age=input('how old are you? ') how old are you? 19 >>> print(age) 19 >>> age>=18 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>=' not supported between instances of 'str' and 'int' >>> age=int(age) >>> age>=18 True再举个栗子:
>>> grade=input() 12.22 >>> grade>=11 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>=' not supported between instances of 'str' and 'int' >>> grade=float(grade) >>> grade>=11 True >>> print(grade) 12.22 >>> grade=int(grade) >>> print(grade) 12前面内容有点少,写这个水一下文章字数
**注意:**Python中for循环用于针对集合中的每个元素的一个代码块,而while循环会不断的运行,直到条件成立。
举个例子:
number = 1 while number<=5: print(number) number=number+1 #就这个模式,别的什么和C/C++没什么区别 #也可以想别的语言一样用break/comtinue,方法差不多,使用条件语句时记得缩进**说明:**for循环时一种遍历列表的有效方式,但是在for循环中不应该修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可以使用while循环。通过将while循环同列表和字典结合起来用,可以收集,存储并组织大量的输入。
在两个列表之间移动元素: 就是把元素从一个列表里pop出来,在append到另一个列表里。
ing=[1,2,3] down=[] while ing: temp=ing.pop() down.append(temp) print('就酱紫')删除包含特定值的所有列表元素:
numbers=[1,2,3,4,1,1,1,1,1] print(numbers) while 1 in numbers: numbers.remove(1) #不得不说这个好方便。。。 print(numbers)