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
import sysdef main(): name = sys.argv[1] if len(sys.argv) > 1 else "world" print(f"Hello, {name}!")if __name__ == "__main__": main()