Skip to main content
Web Fundamentals: Assignment 1 is due at the end of week 2.
My College
On this page

Hello Python

Write a command-line greeter in Python.

Write a Python program that greets the user by name. Replace this with your assignment instructions.

Requirements

  • Accept a name as a command-line argument
  • Print a greeting to stdout
  • Handle the case where no name is provided

Run your program with python main.py and press Ctrl+C to stop it.

Hint

Command-line arguments are available in sys.argv. The first element is the script name.

Solution
Pythonmain.py
import sys
def main():
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"Hello, {name}!")
if __name__ == "__main__":
main()

Last updated: