用户输入和while 循环

input 工作原理

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中。

message = input("need to  input string ")
print(message)


input() 接受一个参数作为提示,程序等待用户输入后,在用户回车确认后继续运行,输入存储在变量中


或者:

promt = "hello plse input you name"
promt += "
you fist name "

input(promt)


使用 int()来获取数值输入

使用函数input()时,Python将用户输入解读为字符串,将输入作为数字使用,使用函数init()

for循环

a = ['ks','dkd','jdjd',10,]
for xx in a:
    print(xx)

求模运算

处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数。
求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少
如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。你可利用这一点来判断一个数是奇数还是偶数

number % 2 == 0:

while循环

current_number = 1
while current_number <= 5:
    print( str(current_number)  + "回合")
    current_number += 1 
    print(current_number)

让用户选择何时退出

prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter 'quit' to end the program. "
 message = ""
 while message != 'quit':
         message = input(prompt)
         print(message) 

break 退出循环

立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句

prompt = "
Please enter the name of a city you have visited:"
prompt += "
(Enter 'quit' when you are finished.) "
print(prompt)

active = True 
while active  :
    xx = input(prompt)
    if xx == "quit" :
        active = False
    else :
        print(xx)
    

break循环

prompt = "
Please enter the name of a city you have visited:"
prompt += "
(Enter 'quit' when you are finished.) "
print(prompt)

while True  :
    xx = input(prompt)
    if xx == "quit" :
        break
    else :
        print(xx)

循环中使用continue

返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环


    current_number = 0
while current_number < 10:
     current_number += 1
     if current_number % 2 == 0:
             continue
     print(current_number) 

首先将current_number设置成了0,由于它小于10,Python进入while循环。进入循环后,我们以步长1的方式往上数 ,因此current_number为1。接下来,if语句检查current_number与2的求模运算结果。如果结果为0(意味着current_number可被2整除),就执行continue语句,
让Python忽略余下的代码,并返回到循环的开头。如果当前的数字不能被2整除,就执行循环中余下的代码,Python将这个数字打印出来


避免无限循环

while 循环处理列表和字典

for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环

unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
while unconfirmed_users:
        current_user = unconfirmed_users.pop() 
        print(current_user.title())
        
        confirmed_users.append(current_user) 
    
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)

总结

break语句可以在循环过程中直接退出循环,而continue语句可以提前结束本轮循环,并直接开始下一轮循环。这两个语句通常都必须配合if语句使用。

使用

用户输入来填充字典

responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
 # 提示输入被调查者的名字和回答
    name = input("
What is your name? ")
    response = input("Which mountain would you like to climb someday? ")

 # 将答卷存储在字典中
    responses[name] = response

 # 看看是否还有人要参与调查
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False

# 调查结束,显示结果
print("
--- Poll Results ---")
for name, response in responses.items():
     print(name + " would like to climb " + response + ".") 
原文地址:https://www.cnblogs.com/g2thend/p/11741452.html