Strings must be inside quotes. Here, Hello is not quoted, so Python treats it as a variable.
Step 2: Identify the error cause
Since Hello is not defined as a variable, this causes a NameError.
Final Answer:
Missing quotes around Hello -> Option D
Quick Check:
Strings need quotes to avoid errors [OK]
Hint: Always put quotes around text strings [OK]
Common Mistakes:
Forgetting quotes around text
Assuming print is misspelled
Thinking variable names cause error
5. You want to create a program that asks a user for their name and then greets them with a message. Which of these code snippets correctly uses strings to do this?
hard
A. name = input(Enter your name: )
print('Hello, ' + name + '!')
B. name = input('Enter your name: ')
print('Hello, ' + name + '!')
C. name = input('Enter your name: ')
print('Hello, name!')
D. name = input('Enter your name: ')
print('Hello, ' + 'name' + '!')
Solution
Step 1: Check input function usage
Input prompt must be a string inside quotes. name = input('Enter your name: ')
print('Hello, ' + name + '!') uses 'Enter your name: ' correctly.
Step 2: Verify greeting message construction
name = input('Enter your name: ')
print('Hello, ' + name + '!') concatenates 'Hello, ' + name + '!' so the user's input is included in the greeting.
Final Answer:
name = input('Enter your name: ')
print('Hello, ' + name + '!') -> Option B
Quick Check:
Input prompt and string concat correct [OK]
Hint: Use quotes for prompts and + to join strings [OK]