早教吧作业答案频道 -->其他-->
Python程序来求救了!又有两个不会、、、2.Writeaprogramthatreadsfromthekeyboardthreenamesfirst,middleandlast.Yourprogramcontainsamainfunctionthatisresponsibleforpromptingtheusertoenterthreenamesallono
题目详情
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.
希望详细编一个。。。
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.
希望详细编一个。。。
▼优质解答
答案和解析
这个不是很难吧。
name = raw_input("Please enter your name")
....
processing the name
....
print 'Nice to meet you:%s' % name
第二个,你就是要注意下
salary:必须先转换成float
还有就是文件最好是一行一行往下读,不然读过的行,要用seek才能返回去读已读的行。
name = raw_input("Please enter your name")
....
processing the name
....
print 'Nice to meet you:%s' % name
第二个,你就是要注意下
salary:必须先转换成float
还有就是文件最好是一行一行往下读,不然读过的行,要用seek才能返回去读已读的行。
看了 Python程序来求救了!又...的网友还看了以下:
求高中英语作文有一天上学的路上,你看到一个抢劫事件,现为公安局写一简单的证词,汇报情况.内容:1. 2020-04-08 …
1.百元面值的人民币上有用荧光物质印的“100”字样,在照射下会显现出来,此外,这种光线在医院、饭 2020-05-20 …
某两位数两个数位上的数之和为11这两位数加上45得到的两位数恰好等于原来的两个数字交换位置所标示的 2020-07-17 …
微观卡特尔:我想知道0.16Q+22.4怎么得来?还有MR=400-0.2Q又是怎么来的?两寡头卡 2020-07-20 …
高中物理解答一个高尔夫球从离海面78M的高空下落,现在计算1秒后的velocity也就是速率吧,最好 2020-10-30 …
快来,急.已知函数f(x)=2√3sin^2x/2+2sinx/2cosx/2-√3①求函数的单调递 2020-12-08 …
1.他充分的利用了他的业余时间2.我应该充分利用你的时间来学习求大神讲这两句话用英语翻译出来1.他充 2020-12-16 …
英语翻译1.洗完的衣服应该晾起来.2.白求恩大夫是个好人,我们永远怀恋他.3.梅梅拾到一个钱包,她把 2020-12-22 …
OO1、27,42各有几个约数,请把它一一写出来.2、求90、216、245的约数个数.3、240与 2020-12-31 …
求原来的两位数.)一个两位数,个位上的数比十位上的数的3倍多2,若把个位数字与十位上的数对换所得新的 2021-01-07 …