Login

Back

How old are you? Answers.

Q1. The program asks for a name and an age. It then prints out this information. It then checks the age and prints out one message if you are older than 14 and a different one if you are 14 or less. Finally, it waits until you press the enter key and exits.
Q2. Line 1 asks you for your name and waits. You have to enter in your name and then press the enter key. When the enter key is pressed, your name is stored in a variable called yourAge.
Q3. When you input a value using an input function, you must remember that it is always a string. Even if you enter e.g. 14, that is not an amount of something. It is a couple of codes, in much the same way as a telephone number is not an amount of something. It is also a set of codes. You cannot test to see if one telephone number is bigger than another because they are not amounts! However, you can convert digits that are codes into digits that are amounts. If you want to convert a code into a whole number, then you need to use int. Line 1 doesn't need int because you are reading in a name, but line 2 needs int because later on, the program will test the size of the value.
Q4. The error message is:

Traceback (most recent call last):
File "C:\Users\admin\Documents\MyPythonPrograms\if.py", line 6, in <module>
if yourAge > 14:
TypeError: unorderable types: str() > int()

It is a very powerful learning technique to introduce controlled errors and to examine the consequences. Python error messages can be rather confusing, but the last line often gives clues that can be understood.
Q5. The comma separates the literal string from the value held in a variable. It also inserts a space in the output.
Q6. Tests might include: a number much great than 14, much less than 14, 14 itself and the numbers bordering 14 (13 and 15). You might also see what happens if someone types in 'nine' instead of 9, or types in a decimal value e.g. 13.5 or types in numbers for their name.
Q7. older than 15 ----    > 15
older than or equal to 15 ----    >= 15
younger than 10 -----   < 10
younger than or equal to 10 ------ <=10
equal to 10 ------     ==10
Q8. You could simply repeat line 8 4 more times. A better way would be:

print("\nYou must stay at home."*5)

There are a range of special escape sequences you can add to printed output inside the quotes. \n moves the cursor to the next line. \t inserts a TAB. You can multiply strings like you can multiply numbers.
Q9. The program would quit immediately it has run. Sometimes, you want to keep the program running so you can observe what is happening. This is the purpose of line 10.
Q10.

yourName = input("What's your name? >>> ")
yourScore = int(input("What's your score? >>> "))
print("Your name is",yourName)
print("Your score was",yourScore,"out of 100.")
if yourScore >= 50:
     print("You passed!")
else:
     print("You failed!")
input("Press <ENTER> to quit ....")

Back