5-3. Alien Colors #1: Imagine an alien was just shot down in a game. Create a variable called alien_color and assign it a value of 'green', 'yellow', or 'red'.
• Write an if statement to test whether the alien’s color is green. If it is, print a message that the player just earned 5 points.
• Write one version of this program that passes the if test and another that fails. (The version that fails will have no output.)
5-4. Alien Colors #2: Choose a color for an alien as you did in Exercise 5-3, and write an if-else chain.
• If the alien’s color is green, print a statement that the player just earned 5 points for shooting the alien.
• If the alien’s color isn’t green, print a statement that the player just earned 10 points.
• Write one version of this program that runs the if block and another that runs the else block.
5-5. Alien Colors #3: Turn your if-else chain from Exercise 5-4 into an if-elif-else chain.
• If the alien is green, print a message that the player earned 5 points.
• If the alien is yellow, print a message that the player earned 10 points.
• If the alien is red, print a message that the player earned 15 points.
• Write three versions of this program, making sure each message is printed for the appropriate color alien.
知识点分析:if-elif-else条件语句简单应用
代码:
#alien_color = 'green'
#alien_color = 'yellow'
alien_color = 'red'
if alien_color == 'green':
print("You earned 5 points")
elif alien_color == 'yellow':
print("You earned 10 points")
else:
print("You earned 15 points")
5-8. Hello Admin: Make a list of five or more usernames, including the name 'admin'. Imagine you are writing code that will print a greeting to each user after they log in to a website. Loop through the list, and print a greeting to each user:
• If the username is 'admin', print a special greeting, such as Hello admin, would you like to see a status report?
• Otherwise, print a generic greeting, such as Hello Eric, thank you for logging in again.
5-9. No Users: Add an if test to hello_admin.py to make sure the list of users is not empty.
• If the list is empty, print the message We need to find some users!
• Remove all of the usernames from your list, and make sure the correct message is printed.
5-10. Checking Usernames: Do the following to create a program that simulates how websites ensure that everyone has a unique username.
• Make a list of five or more usernames called current_users.
• Make another list of five usernames called new_users. Make sure one or two of the new usernames are also in the current_users list.
• Loop through the new_users list to see if each new username has already been used. If it has, print a message that the person will need to enter a new username. If a username has not been used, print a message saying that the username is available.
• Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.
知识点分析:if语句在列表中的应用,空列表判断
代码:
#5-8
users = ['admin', 'Alice', 'Bob', 'Carol', 'Denis']
for user in users:
if user == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello "+user+", thank you for logging in again.")
#5-10
new_users = ['Zachary', 'Alice', 'Eddy', 'CAROL', 'Charlie']
current_users = [user.lower() for user in users]
for user in new_users:
if user.lower() not in current_users:
print("available")
else:
print("Occupied! Please enter a new username")
#5-9
if users:
print("Not empty")
else:
print("We need to find some users!")
#del users
for i in range(0,5):
users.pop()
'''
#Invalid Usage:
for users:
users.pop()
'''
if users:
print("Not empty")
else:
print("We need to find some users!")
6-2. Favorite Numbers: Use a dictionary to store people's favorite numbers. Think of five names, and use them as keys in your dictionary. Think of a favorite number for each person, and store each as a value in your dictionary. Printeach person's name and their favorite number. For even more fun, poll a few friends and get some actual data for your program.
6-10. Favorite Numbers: Modify your program from Exercise 6-2 (page 102) so each person can have more than one favorite number. Then print each person's name along with their favorite numbers.
知识点分析:字典项基本访问与遍历,列表字典
代码:
#6-2
nums = {'Alice':11, 'Bob':8, 'Carol':100, 'Bob':255, 'Eddy':1024}
for name, num in nums.items():
print(name+": "+str(num))
#6-10
nums = {'Alice':[11,22], 'Bob':[8,16], 'Carol':[100,128], 'Bob':[255,256,512], 'Eddy':[1,1024]}
for name, num in nums.items():
print(name+": ")
for n in num:
print(n)
6-5. Rivers: Make a dictionary containing three major rivers and the country each river runs through. One key-value pair might be 'nile': 'egypt'.• Use a loop to print a sentence about each river, such as The Nile runs through Egypt.
• Use a loop to print the name of each river included in the dictionary.
• Use a loop to print the name of each country included in the dictionary.
知识点分析:字典项、键、值的遍历
代码:
places = {'nile':'egypt', 'pearl river':'china', 'river thames':'britian'}
for river, country in places.items():
print('The '+river.title()+' runs through '+country.title())
for river in places.keys():
print('The '+river.title())
for country in places.values():
print(country.title())
6-11. Cities: Make a dictionary called cities. Use the names of three cities as keys in your dictionary. Create a dictionary of information about each city and include the country that the city is in, its approximate population, and one fact about that city. The keys for each city’s dictionary should be something like country, population, and fact. Print the name of each city and all of the information you have stored about it.
知识点分析:在字典中存储字典
代码:
cities = {
'Guangzhou':{'country':'China', 'population':14498400, 'fact':'Flower City'},
'Shanghai':{'country':'China', 'population':24183300, 'fact':'Modern City'},
'Beijing':{'country':'China', 'population':21707000, 'fact':'Capital of China'}}
for city, info in cities.items():
print(city+':')
for item, content in info.items():
print(item.title()+': '+str(content))