Bird
0
0

You want to create a program that asks for a user's name and age, then prints a message including both. Which code correctly handles input and output?

hard📝 Application Q9 of 15
Python - Input and Output
You want to create a program that asks for a user's name and age, then prints a message including both. Which code correctly handles input and output?
Aprint(name + ' is ' + age + ' years old')
Bname = input('Name: ') age = int(input('Age: ')) print(name + ' is ' + age + ' years old')
Cname = input('Name: ') age = input('Age: ') print(name + ' is ' + int(age) + ' years old')
Dname = input('Name: ') age = input('Age: ') print(f'{name} is {age} years old')
Step-by-Step Solution
Solution:
  1. Step 1: Check input and output handling

    Input returns strings; age can remain string for display. Using f-string simplifies output.
  2. Step 2: Evaluate options for correct syntax and output

    name = input('Name: ') age = input('Age: ') print(f'{name} is {age} years old') uses input correctly and prints with f-string, which handles variables well.
  3. Final Answer:

    name = input('Name: ')\nage = input('Age: ')\nprint(f'{name} is {age} years old') -> Option D
  4. Quick Check:

    Use f-strings for clean output with variables [OK]
Quick Trick: Use f-strings to combine variables in output [OK]
Common Mistakes:
MISTAKES
  • Adding int to string without conversion
  • Not defining variables before print
  • Using print without variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes