Final challenge: different answers for different inputs
After this: Predict, fix and complete programs that choose between paths.
This challenge has four steps, and each step is scored on its own. You will predict what a chain prints, fix a chain whose cases are in the wrong order, write a condition that checks a range, and work out three joined conditions by hand. Every step shows the code in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 4Predict it
Predict it
A ticket price depends on age. What does this print for age 70?
age = 70if age < 5: print("Free")elif age < 18: print("Child")elif age >= 65: print("Senior")else: print("Adult")int age = 70;if (age < 5) { System.out.println("Free");} else if (age < 18) { System.out.println("Child");} else if (age >= 65) { System.out.println("Senior");} else { System.out.println("Adult");}Fix the order
This chain is meant to give A for 90 or more, B for 80 or more, C for 70 or more and D otherwise. It gives 95 a C. Which one change makes it right?
score = 95if score >= 70: print("C")elif score >= 80: print("B")elif score >= 90: print("A")else: print("D")Output
C
int score = 95;if (score >= 70) { System.out.println("C");} else if (score >= 80) { System.out.println("B");} else if (score >= 90) { System.out.println("A");} else { System.out.println("D");}Output
C
Fill the condition
Complete the if line so that In range is printed when n is between 1 and 10, both ends included. You will need to join two comparisons.
n = 7____________________ # ← is n between 1 and 10? print("In range")else: print("Out of range")int n = 7;____________________ // ← is n between 1 and 10? System.out.println("In range");} else { System.out.println("Out of range");}Combine it
Work out each line by hand. What does this print?
x = 7print(x > 5 and x < 10)print(x < 5 or x == 7)print(not (x == 7))int x = 7;System.out.println(x > 5 && x < 10);System.out.println(x < 5 || x == 7);System.out.println(!(x == 7));- Predict what a chain prints, fix the order of a chain, complete a condition that checks a range, and work out and, or and not by hand.