The first homework is to create a new project called "MyOwnCommandLineArguments" as following: Receive the ages of the all your family members (between 3 to 6 members) as command line arguments in the format as following (name then age) Monica 12 Daniel 34 Shelley 23 Compute and display the average of the ages that were entered.
时间: 2023-12-03 20:46:02 浏览: 78
Sure, I can help you with that!
To start, create a new project in your preferred programming language. Then, you'll need to access the command line arguments passed to your program.
In Python, you can use sys.argv to access the command line arguments. Here's some example code:
```
import sys
# Get command line arguments
args = sys.argv[1:]
# Parse arguments into dictionary
family = {}
for i in range(0, len(args), 2):
family[args[i]] = int(args[i+1])
# Calculate average age
total_age = sum(family.values())
average_age = total_age / len(family)
# Print results
print("Family ages:", family)
print("Average age:", average_age)
```
This code takes the command line arguments, which are assumed to be in the format "name age", and parses them into a dictionary. Then, it calculates the total age and average age of the family members, and prints out the results.
To run this code, you would call it from the command line like this:
```
python my_program.py Monica 12 Daniel 34 Shelley 23
```
This would output:
```
Family ages: {'Monica': 12, 'Daniel': 34, 'Shelley': 23}
Average age: 23.0
```
I hope this helps you get started on your homework! Let me know if you have any questions.
阅读全文