教材:《Python编程 从入门到实践》
5-3:外星人颜色#1
#5-3 Color of aliens #1 alien_color = "green" if alien_color == "green": print("You've got 5 points!") alien_color = "yellow" if alien_color == "green": print("Congratz on getting 5 points!")
输出:
You've got 5 points!
5-4:外星人颜色#2
#5-3 Color of aliens #2 alien_color = "green" if alien_color == "green": print("You've got 5 points!") else: print("You've got 10 points!") alien_color = "yellow" if alien_color == "green": print("Congratz on getting 5 points!") else: print("Congratz on getting 10 points!")
输出:
You've got 5 points! Congratz on getting 10 points!
5-5:外星人颜色#3
#5-3 Color of aliens #2 alien_color = "green" if alien_color == "green": print("You've got 5 points!") elif alien_color == "yellow": print("You've got 10 points!") else: print("You've got 15 points!") alien_color = "yellow" if alien_color == "green": print("Congratz on getting 5 points!") elif alien_color == "yellow": print("Congratz on getting 10 points!") else: print("Congratz on getting 15 points!") alien_color = "red" if alien_color == "green": print("You've got 5 points!") elif alien_color == "yellow": print("You've got 10 points!") else: print("You've got 15 points!")
输出:
You've got 5 points! Congratz on getting 10 points! You've got 15 points!