Python 程序来求救了!又有两个不会、、、

Python 程序来求救了!又有两个不会、、、
2. Write a program that reads from the keyboard three names first, middle and last. Your program contains a main function that is responsible for prompting the user to enter three names all on one line and accept all three names as a single string. In your main function call another function (that you must also create) called initials and pass the string that you read. In the initials function you should parse through the name and convert the first name and middle name to initials complete with periods. Concatenate the two initials together along with the last name as a single string.
As an example, the name Jean Luc Picard would become J. L. Picard.
While you are at it, make sure that case of all of the names is correct. For example jeAn mARk kirk should become J. M. Kirk. Have your initials function return the new single string back to your main method for printing. Hint: Assume that the user always enters in three names. You will need to figure out where each name begins and ends. You do not need to loop this program. Work in very small steps. Separate the names out before you do anything else. Then worry about capturing the first initial, then worry about the upper/lower case. My output from two separate runs:
python question2.py
Please enter your name:Jean luc pICARD
Nice to meet you: J. L. Picard
python question2.py
Please enter your name:jeAn mARk KIRK
Nice to meet you: J. M. Kirk
3. Each line of data in a given file contains a person's employee id, their salary and their department (example below). Your employer wants to have answers to the following questions: 1) what is the average salary in the company, 2) what are the maximum and minimum salaries in the company 3) How many employees are there in each department.
To determine this, do the following. Read the data from the file one line at a time. Split the data into fields (separated by ':') and create two lists, one that contains all of the salaries, and one that contains all of the department information. Iterate through the salaries list (in a for loop) to determine the max, min and average salaries. Then iterate through the department list and determine the distribution of employees by department (Hint: There are only three departments: IT, Development and Administration).
You can do all of these tasks within a single main method. Catch any IOError exceptions that might occur and to be safe, catch any ValueError exceptions that might occur with respect to the salary data (in either case you may simply print an appropriate error message and terminate the program).
Test your program with provided file company.txt
python question6.py
Enter filename to process:company.txt
Salaries - Max: 124993.94 Min: 25015.53 Avg: 75041.0493683
Employees per dept - IT: 3398 Development: 3325 Administration: 3281
python question6.py
Enter a filename to process:somefilethatdoesnotexist.txt
A file IO error has occurred.
希望详细编一个。。。
yjoe 1年前 已收到1个回答 举报

熊茗 花朵

共回答了14个问题采纳率:100% 举报

这个不是很难吧。
name = raw_input("Please enter your name")
....
processing the name
....
print 'Nice to meet you:%s' % name
第二个,你就是要注意下
salary:必须先转换成float
还有就是文件最好是一行一行往下读,不然读过的行,要用seek才能返回去读已读的行。

1年前

9
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.037 s. - webmaster@yulucn.com