PythonCrashCourse Chapter 5 Exercises

5.1 Write a series of conditional tests; print each test and your predictions and actual results. The code you write should look similar to the following:

car = 'subaru'
print("Is car == 'subaru'? I predict True.") print(car == 'subaru')

print("\nIs car == 'audi'? I predict False.") print(car == 'audi')
health = "great"
print("Is health == 'great'? I predict True.")
print(car == 'great')
print("\nIs health == 'bad'? I predict False.")
print(car == 'bad')

5.2 You are not limited to 10 tests. If you want to try more comparisons, you can write some more tests and add them to conditional_tests.py. For the various tests listed below, write at least one test that results in True and False

  • Check that two strings are equal and not equal.
  • Test using the function lower ().
  • Check that the two numbers are equal, unequal, greater than, less than, greater than or equal, and less than or equal.
  • Test using keywords and and or.
  • Test whether a specific value is included in the list.
  • Test whether a specific value is not included in the list.
car = "Audi"
print("Is car =='BWM' I predict False.")
print(car == 'BWM')
print(car == 'Audi')
print("Is car =='audi'",(car.lower()=='audi'))
numbers = 24
print("numbers >= 20:")
print(numbers >= 20)
print("numbers <= 20:")
print(numbers <= 20)
print("numbers >= 20:")
print(numbers >= 20)
print("numbers < 20:")
print(numbers < 20)
print(numbers <= 25 and numbers >= 24)
print(numbers >= 25 or numbers <= 23)
number_values= [10,23,54,54]
print(10 in number_values)
print(11 not in number_values)

5.3 Suppose you just shot an alien in the game, please create a variable named alien_color and set it to 'green', 'yellow' or 'red'.

  • Write an if statement to check if the alien is green; if it is, print a message indicating that the player earned 5 points.
  • Write two versions of this program, in one version the above test passed, but in the other version failed (no output when failed test).
alien_colors = ['green','yellow','red']
alien_color =alien_colors[0]
if alien_color == 'green':
	print("you just earned 5 points")

alien_color =alien_colors[1]
if alien_color == 'green':
	print("you just earned 5 points")
else:
	print("you are not earned 5 points")

5.4 Set the alien color as in Exercise 5-3 and write an if-else structure.

  • If the alien is green, print a message stating that the player earned 5 points for shooting the alien
  • . If the alien is not green, print a message indicating that the player earned 10 points.
  • Write two versions of this program, execute the if code block in one version, and the else code block in the other version.
alien_colors = ['green','yellow','red']
alien_color =alien_colors[0]
if alien_color == 'green':
	print("you just earned 5 points")
elif alien_color =='yellow':
	print("you just earned 10 points")
elif alien_color == 'red':
	print("you just earned 15 points")

alien_color =alien_colors[1]
if alien_color == 'green':
	print("you just earned 5 points")
elif alien_color =='yellow':
	print("you just earned 10 points")
elif alien_color == 'red':
	print("you just earned 15 points")

alien_color =alien_colors[2]
if alien_color == 'green':
	print("you just earned 5 points")
elif alien_color =='yellow':
	print("you just earned 10 points")
elif alien_color == 'red':
	print("you just earned 15 points")

5.5 Change the if-else structure in Exercise 5-4 to if-elif-else structure.

  • If the alien is green, print a message indicating that the player earned 5 points.
  • If the alien is yellow, print a message indicating that the player earned 10 points.
  • If the alien is red, print a message indicating that the player earned 15 points.
  • Write three versions of this program, they print a message when the alien is green, yellow and red.
alien_colors = ['green','yellow','red']
alien_color =alien_colors[0]
if alien_color == 'green':
	print("you just earned 5 points")
elif alien_color =='yellow':
	print("you just earned 10 points")
elif alien_color == 'red':
	print("you just earned 15 points")

alien_color =alien_colors[1]
if alien_color == 'green':
	print("you just earned 5 points")
elif alien_color =='yellow':
	print("you just earned 10 points")
elif alien_color == 'red':
	print("you just earned 15 points")

alien_color =alien_colors[2]
if alien_color == 'green':
	print("you just earned 5 points")
elif alien_color =='yellow':
	print("you just earned 10 points")
elif alien_color == 'red':
	print("you just earned 15 points")

5.6 Set the value of the variable age, and then write an if-elif-else structure, according to the value of age to determine which stage of life.

  • If a person is younger than 2 years old, print a message stating that he is a baby.
  • If a person's age is 2 (inclusive) to 4 years old, print a message that he is a toddler.
  • If a person's age is 4 (inclusive) to 13 years old, print a message that he is a child.
  • If a person's age is 13 (inclusive) ~ 20 years old, print a message stating that he is a teenager.
  • If a person's age is 20 (inclusive) ~ 65 years old, print a message stating that he is an adult.
  • If a person's age is over 65 years old, print a message stating that he is an elderly person.
age = 24
if age < 2:
	print("the person is a baby.")
if age < 4:
	print("the person is a toddler.")
if age < 13:
	print("the person is a kid.")
if age < 20:
	print("the person is a teenager.")
if age < 65:
	print("the person is an adult.")
if age >= 65:
	print("the person is an elder.")

5.7 Create a list that contains the fruits you like, and then write a series of independent if statements to check whether the list contains specific fruits.

  • Name the list favorite_fruits and include three fruits in it.
  • Write 5 if statements, each of which checks whether a certain fruit is included in the list, if it is included in the list, print a message, such as "You really like bananas!".
fruits = ['cucumber','apple','strawberry','banana']
if 'apple' in fruits:
	print("You really like apple.")
if 'banana' in fruits:
	print("You really like banana.")
if 'strawberry' in fruits:
	print("You really like strawberry.")
if 'cucumber' in fruits:
	print("You really like cucumber.")
if 'pear' in fruits:
	print("You really like pear.")

5.8 Create a list with at least 5 user names, and one of the user names is 'admin'. Imagine you want to write code to print a greeting message after each user logs in to the website. Iterate through the list of user names and print a greeting message to each user.

  • If the user name is 'admin', print a special greeting message, such as "Hello admin, would you like to see a status report?".
  • Otherwise, print a normal greeting message, such as "Hello Eric, thank you for logging in again".
usernames = ["joey","chandler","monica","ross","rachel","phoebe","admin"]
for name in usernames:
	if name == 'admin':
		print(f"Hello,{name}, would you like to see a status report.")
	else:
		print(f"Hello {name},thank you fo logging in again.")

5.9 In the program written to complete exercises 5-8, add an if statement to check whether the list of user names is empty.

  • If it is empty, the message "We need to find some users!" Is printed.

  • Delete all user names in the list and make sure that the correct message will be printed.

usernames = []
if not usernames:
	print("We need to find some users!")

5.10 Follow the instructions below to write a program that simulates the way the website ensures that each user's username is unique.

  • Create a list with at least 5 user names and name it current_users. Create another list with 5 usernames, name it new_users, and make sure that one or two usernames are also included in the list current_users.

  • Iterate through the list new_users, and for each user name in it, check if it has been used. If so, print a message indicating that another user name needs to be entered; otherwise, print a message indicating that this user name is not used.

  • Make sure that big messages are not distinguished when comparing; in other words, if the username 'John' is already used, the username 'JOHN' should be rejected

current_users = ["joey","chandler","monica","ross","rachel","phoebe","admin"]
new_users =["Joey","admin","susan","tom"]
for user in new_users:
	if user.lower() in current_users:
		print(f"you need a new username.{user}")
	else:
		print(f"the username is available.{user}")

5.11 Ordinal numbers indicate positions, such as 1st and 2nd. Most ordinal numbers end in th, with the exception of 1, 2, and 3.

  • Store the numbers 1 ~ 9 in a list. Traverse this list.

  • Use an if-elif-else structure in the loop to print the ordinal number corresponding to each number. The output should be 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, and 9th, but each ordinal number is on its own line.

numbers =[1,2,3,4,5,6,7,8,9,10]
for nubmer in numbers:
	if nubmer == 1:
		print("1st")
	elif nubmer == 2:
		print("2nd")
	elif nubmer == 3:
		print("3rd")
	else:
		print(f"{nubmer}th")

Guess you like

Origin www.cnblogs.com/CodingXu-jie/p/12731296.html