# Python Crash Course读书笔记 - 第7章：USER INPUT AND WHILE LOOPS

## input函数工作原理

>>> message=input("your name:")
>>> print(message)
steven


>>> age = input("How old are you? ")
How old are you? 18
>>> age = int(age)
>>> age += 2
>>> age
20


>>> a = float(1.2)
>>> a
1.2


>>> 11%2
1
>>> 12%2
0


## while循环

$cat months.py months = ['jan', 'feb', 'march', 'apr'] i=0 while i < 4: print(months[i].title()) i += 1$ python3 months.py
Jan
Feb
March
Apr


$cat echo.py message = "" while message != 'quit': message = input("you input:") if message != 'quit': print(message)$ python3 echo.py
you input:hello
hello
you input:world
world
you input:quit


$cat echo.py active = True while active: message = input("you input:") if message == 'quit': active = False else: print(message)$ python3 echo.py
you input:hello
hello
you input:world
world
you input:quit


while True:
message = input("you input:")
if message == 'quit':
break
else:
print(message)


## while循环与List和字典结合

$cat whilelist.py todolist = ['task1', 'task2', 'task3'] donelist = [] # empty list while todolist: donelist.append(todolist.pop()) print(f"Done List is: {donelist}") print(f"Todo List is: {todolist}")$ python3 whilelist.py
Todo List is: []


$cat removeevil.py gods = ['zeus','evil', 'athena', 'evil', 'apollo', 'hera', 'iris'] toremove = 'evil' while toremove in gods: gods.remove(toremove) print(gods)$ python3 removeevil.py
['zeus', 'athena', 'apollo', 'hera', 'iris']


©️2019 CSDN 皮肤主题: 撸撸猫 设计师: 设计师小姐姐