The larger of two numbers
Which of two numbers is larger? Decomposition: read a, read b, compare, show one of them. The comparison a > b is true when a is larger. Python has more signs: < for smaller, >= for larger or equal, <= for smaller or equal, == for equal. The program shows a if a > b is true, and b if it is false. What if the numbers are equal, for example 9 and 9? Then a > b is false, so the else branch runs and shows b. This is still right, because b is 9 and the larger number is 9. The code shows a good habit: think about the border case, here equal numbers, before you trust the program.
1a = int(input("First number: ")) 2b = int(input("Second number: ")) 3if a > b: 4 print(a) 5else: 6 print(b)
First number: 8 Second number: 15 15
1a = int(input("First number: ")) 2b = int(input("Second number: ")) 3if a > b: 4 print(a) 5else: 6 print(b)
First number: 9 Second number: 9 9
Change the program on paper so that it shows the smaller number. Which sign do you change? Test it with 12 and 5, and with 7 and 7. Then write a program idea that says Pass when a mark is at least 50.