What your program needs to do
Your program should:
- Ask the user for their name.
- Save the name in a variable.
- Print a polite message using that name.
-
Put all working code inside
main(). -
Use
if __name__ == "__main__": -
Have:
- one module docstring
- one main() docstring
-
Use
+for string concatenation, not f-strings.
Step 1: Add a module docstring
A module docstring goes at the very top of the program.
Example:
"""Ask the user for their name and print a polite greeting."""
This explains what the whole program does.
Step 2: Create the main() function
Your program needs a main() function because the assignment says all statements should be inside it.
Example:
def main():
Step 3: Add a docstring for main()
The assignment says the main() docstring should be a complete command-form sentence.
That means it should sound like an instruction.
Example:
"""Prompt the user for a name and print a polite response."""
Step 4: Ask for the user’s name with input()
Use input() to ask for the name and store it in a variable.
Example:
name = input("Please enter your name: ")
-
nameis the variable -
input()waits for the user to type something
Step 5: Build the response using concatenation
The assignment says you must use the + operator.
So do this:
message = "Hello, " + name + "."
This joins:
-
"Hello, " - the user’s name
-
"."
into one string.
If the user types Kyaw, the result becomes:
"Hello, Kyaw."
Step 6: Print the result
Now print the message:
print(message)
Step 7: Use the if __name__ == "__main__": pattern
At the bottom of the program, write:
if __name__ == "__main__":
main()
This tells Python to run main() when the file is executed.
Full example
Since your sample run is not shown in your message, here is a basic correct example format:
"""Ask the user for their name and print a polite greeting."""
def main():
"""Prompt the user for a name and print a polite response."""
name = input("Please enter your name: ")
message = "Hello, " + name + "."
print(message)
if __name__ == "__main__":
main()
How each line works
"""Ask the user for their name and print a polite greeting."""
This is the module docstring.
def main():
This starts the main function.
"""Prompt the user for a name and print a polite response."""
This is the docstring for main().
name = input("Please enter your name: ")
This asks the user to type their name.
message = "Hello, " + name + "."
This uses concatenation with + to build the output.
print(message)
This prints the greeting.
if __name__ == "__main__":
main()
This runs the program.
PEP-8 things to remember
For this assignment, PEP-8 means:
-
Use lowercase function names like
main - Put blank lines in the correct places
- Indent with 4 spaces
-
Use spaces around
= - Keep code neat and easy to read
Example:
name = input("Please enter your name: ")
This is better than:
name=input("Please enter your name: ")
Important note
You said the punctuation and spacing must match the sample run exactly, but I do not see the sample run in your message.
That means your teacher may want something slightly different, like:
What is your name? Kyaw
It is nice to meet you, Kyaw.
or maybe another exact sentence.
So the structure will stay the same, but the words inside input() and print() must match the sample exactly.
Paste the sample run here, and I’ll rewrite the program to match it exactly.
No comments:
Post a Comment