Exercise 1: Alien colors 1: Suppose you just shot an alien in the game, please create an alien named alien_color and set it to 'green', 'yellow', or 'red'. Write an if statement to check whether aliens are green; if so, print a message indicating that the player has obtained 5 points. Write two versions of this program. In one version, the above test passes, while in the other version, it fails (there is no output when the test fails).
(1) alien_color='green' if 'green' == alien_color: print('Players gain 5 points') (2) alien_color='yellow' if 'green' == alien_color: pass else: pass
Exercise 2: Alien colors 2: Set the alien color as in exercise 1 and write an if else structure. If the alien is green, print a message indicating that the player has obtained 5 points for shooting the alien. If the alien is not green, print a message indicating that the player has obtained 10 points. Write two versions of the program, executing if blocks in one version and else blocks in the other.
(1) alien_color='green' if 'green' == alien_color: print('Five points were obtained') else: print('Players gain 10 points') #Five points were obtained (2) alien_color='green' if 'green' != alien_color: print('Five points were obtained') else: print('Players gain 10 points') #Players gain 10 points
Exercise 3: Alien colors 3: Change the if else structure in Exercise 2 to the if elif else structure. If the alien is green, print a message indicating that the player has obtained 5 points. If the alien is yellow, print a message indicating that the player has obtained 10 points. If the alien is red, print a message indicating that the player has obtained 15 points. Write three versions of this program, which print a message when the alien is green, yellow and red.
(1) alien_color='green' if 'green'==alien_color: print('Players gain 5 points') elif 'yellow'==alien_color: print('Players gain 10 points') else: print('The player gained 15 points') #Players gain 5 points (2) alien_color='yellow' if 'green'==alien_color: print('Players gain 5 points') elif 'yellow'==alien_color: print('Players gain 10 points') else: print('The player gained 15 points') #Players gain 10 points (3) alien_color='red' if 'green'==alien_color: print('Players gain 5 points') elif 'yellow'==alien_color: print('Players gain 10 points') else: print('The player gained 15 points') #The player gained 15 points
Exercise 4: different stages of life: set the value of the variable age, and then write an if elif else structure to judge which stage of life you are in according to the value of age. If a person is less than 2 years old, print a message indicating that he is a baby. If a person's age is (inclusive) to 4 years old, print a message indicating that he is toddlering. If a person's age is 4 (inclusive) to 13 years old, print a message indicating that he is a child. If a person's age is 13 (inclusive) to 20 years old, print a message indicating that he is a teenager. If a person's age is 20 years old From (inclusive) to 65 years old, print a message indicating that he is an adult. If a person is over 65 years old, print a message indicating that he is an elderly person.
age=24 if age<2: print('He is a baby') elif 2<=age<4: print('Toddler') elif 4<=age<13: print('children') elif 13<=age<20: print('teenagers') elif 20<=age<65: print('adult') else: print('aged') #adult
Ordinal number: ordinal numbers represent positions, such as 1st and 2nd. Most ordinals end with th, with the exception of 1, 2, and 3. Store the numbers 1 to 9 in a list. Traverse the list. An if elif else structure is used in the loop to print the ordinal number corresponding to each number. The output content should be 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th and 9th, but each ordinal number is exclusive.
list1=[1,2,3,4,5,6,7,8,9] for i in list1: if i==1: print('1st') elif i==2: print("2nd") elif i==3: print("3nd") else: print(i) ''' 1st 2nd 3nd 4 5 6 7 8 9 '''