Python学习笔记第八天

每日一句:没有一件工作是旷日持久的,除了那件你不敢拌着手进行的工作。

在字典中存储列表

# 存储所点披萨的信息
pizza={
    'crust':'thick',
    'toppings':['mushroom','extra chease'],
# 键的值可以是列表
}
# 概述所点的披萨
print("You ordered a "+pizza['crust']+"-crust pizza "+"with the following toppings:")
for topping in pizza['toppings']:
    print("\t"+topping)
# 遍历列表
You ordered a thick-crust pizza with the following toppings:
	mushroom
	extra chease
favorite_languages={
    'jen':['python','go'],
    'sarah':['c','java'],
    'edward':['rudy'],
    'phil':['java','js','Ts'],
    'lisa':['java']
}
for name,languages in favorite_languages.items():
    print("\n"+name.title()+"'s favorite languages are:")
    for language in languages:
        print(language.title())
Jen's favorite languages are:
Python
Go

Sarah's favorite languages are:
C
Java

Edward's favorite languages are:
Rudy

Phil's favorite languages are:
Java
Js
Ts

Lisa's favorite languages are:
Java

在字典中存储字典

users={
    'zhangsan':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton',
    },
    'lisi':{
        'first':'marie',
        'last':'curie',
        'location':'paris',
    }
}
for username,user_info in users.items():
    print("\nUsername:"+username)
    full_name=user_info['first']+" "+user_info['last']
    location=user_info['location']
    print("\tFull name:"+full_name.title())
    print("\tlocation:"+location.title())
Username:zhangsan
	Full name:Albert Einstein
	location:Princeton

Username:lisi
	Full name:Marie Curie
	location:Paris


用户输入和while循环

用户输入input()

message=input("请输入用户名:")
print("姓名:"+message)
# input() 用户可以在输入框进行输入
# pyrhon2.x 得用raw_input
请输入用户名:李四
姓名:李四
height=input("Hello tall are you,in inches? ")
height=int(height)
if height>=36:
    print("\nUou're tall enough to ride! " )
else:
    print("\nYou'll be able to ride when you're a little older.")
Hello tall are you,in inches? 18

You'll be able to ride when you're a little older.
# % 可以用于判断是技术还是偶数
num=int(input("请输入数字: "))
if num % 2==0:
    print("该数字为偶数 !!!")
else:
    print("该数字为奇数 !!!")
请输入数字: 45
该数字为奇数 !!!

while循环

current_number = 1
while current_number<=5:
    print(current_number)
    current_number+=1
# while 条件:
# 只有满足条件,while语句才会运行
1
2
3
4
5
prompt="\nTell me something, and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the progrem."
message=""
while message !='quit':
    message=input(prompt)
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.sa

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.quit
prompt="\nTell me something, and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the progrem."
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 progrem.wsd
wsd

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.quit

使用标志

prompt="\nTell me something, and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the progrem."
active=True
while active:
    message = input(prompt)
    if message=='quit':
        active=False
    else:
        print(message)
# active标志,类似开关,简化了while需要判断的过程
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.wxd
wxd

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.quit

break

prompt="\nTell me something, and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the progrem."
while True:
    city=input(prompt)
    if city=='quit':
        break
    else:
        print("I'd love to go to "+city.title()+"!")
# break打断,当当条件满足,则退出循环,在任何循环都可以使用,来退出循环
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.beijing
I'd love to go to Beijing!

Tell me something, and I will repeat it back to you:
Enter 'quit' to end the progrem.quit

continue

num1=0
while num1<10:
    num1+=1
    if num1%2==0:
        continue
    print(num1)
# 当num1小于10时,num1自增1,当num为偶数是,回到最开始,判断num1是否小于10,当num1为奇数,则直接打印出
1
3
5
7
9

避免无限循环

x=1
while x<=5:
    print(x)
    x+=1
# 当x+=1漏掉,将造成程序无限循环下去,因为while判断x=1一直是小于5的,程序会一直运行下去
1
2
3
4
5

while循环来处理列表和字典

# 1.创建一个待验证的用户列表
# 2。一个用于存储已验证的列表
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 confiremed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confiremed:
Candace
Brian
Alice

删除包含特定值的所有列表元素

pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
    print(pets)
print("-----")
print(pets)
# 删除pets中所有的cat
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
-----
['dog', 'dog', 'goldfish', 'rabbit']

用户输入填充字典

responses={}
# 设置一个标志,指出调查是否继续
polling_active=True
while polling_active:
    # 提示输入被调查的名字和回答  
    name=input("\nWhat 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("\n---Poll Results---")
for name,response in responses.items():
    print(name+" would like to climb "+response+".")
What is your name?川建国
Which mountain would you like to climb someday?武夷山
Would you like to let another person respond?(yes/no)yes

What is your name?王五
Which mountain would you like to climb someday?泰山
Would you like to let another person respond?(yes/no)no

---Poll Results---
川建国 would like to climb 武夷山.
王五 would like to climb 泰山.

 

猜你喜欢

转载自www.cnblogs.com/python-study-notebook/p/12694784.html