码迷,mamicode.com
首页 > 其他好文 > 详细

用户输入和while循环

时间:2019-01-12 00:25:10      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:verify   str   利用   rom   rem   eth   response   列表   person   

input()

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nwhat is your first name? "

name = input(prompt)
print("\nhello, " + name + "!")

输出:
If you tell us who you are, we can personalize the messages you see.
what is your first name? Eric

hello, Eric!

求模运算符:%,它将两个数相除并返回余数

print(4%3)
print(6%3)
print(1%3)
print(-1%3)  #为啥是2
输出: 
1
0
1
2

余数如果是0,可利用这点来判断奇/偶数

number = input("Enter a number, and I‘ll tell you if it‘s even or odd: ")
number = int(number)
if number % 2 == 0:   #numbber % 2 == 0 ,number就是偶数,否则是奇数
    print("\nThe number " + str(number) + " is even.")
else:
    print("\nThe number " + str(number) + " is odd.")
输出:
Enter a number, and I‘ll tell you if it‘s even or odd: 5

The number 5 is odd.

while循环:不断地运行,知道指定的条件不满足为止。

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)
输出:
Tell me something, and I will repeat it back to you:
Enter quit to end the program.hello everyone!
hello everyone!

Tell me something, and I will repeat it back to you:
Enter quit to end the program.hello again!
hello again!
Tell me something, and I will repeat it back to you: 
Enter
quit to end the program.quit

标记

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)

break

prompt = "\nPLS enter the 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("I‘d love to go to " + city.title() + "!")
输出:

PLS enter the name of a city you have visited:
(Enter quit when you are finished.) Beijing
Id love to go to Beijing!

PLS enter the name of a city you have visited:
(Enter quit when you are finished.) ShenYang
Id love to go to Shenyang!

PLS enter the name of a city you have visited:
(Enter quit when you are finished.) quit

continue

current_number = 0

while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    
    print(current_number)
        
输出:
1
3
5
7
9

注:如果命令行出现无限循环,Ctrl+C退出

验证新注册用户,将未验证用户移动到已验证用户列表中

unconfirmed_users = [alice,brian,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 users have been confirmed: ")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
    
输出:
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice

删除列表中所有的指定元素

pets = [dog,cat,dog,goldfish,cat,rabbit,cat]
print(pets)

while cat in pets:
    pets.remove(cat)

print(pets)
输出:
[dog, cat, dog, goldfish, cat, rabbit, cat]
[dog, dog, goldfish, rabbit]

 

responses = {}

active = True

while active:
    name = input("what‘s your name? ")
    response = input("what‘s your favorite language:")
    
    responses[name] = response
    
    repeat = input("go on?(yes/no)")
    if repeat == no:
        active = False
        
print("\n -- Result -- ")
for name,response in responses.items():
    print(name.title() + "‘s favorite language is :" + response.title())
输出:
whats your name? alex
whats your favorite language:python
go on?(yes/no)yes
whats your name? suki
whats your favorite language:C
go on?(yes/no)no

 -- Result --
Alexs favorite language is :Python
Sukis favorite language is :C

 

用户输入和while循环

标签:verify   str   利用   rom   rem   eth   response   列表   person   

原文地址:https://www.cnblogs.com/leisurelyRD/p/10256685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!