python代码辅导讲解、讲解python代码、辅导python代码, 辅导python代码编程

- 首页 >> Python编程


Q1 Write a Python program that asks the user to enter a full name in the following

format :LastName, FirstName MiddleName (for Example: "Hun, Attila The”). The

input will have a single space after the comma and only a single space between

the first name and the middle name. The last name and first name should not be

more than 10 characters long. You are to use the methods in the str class in Python

to convert the input string to a new String having the form:

<FirstName><MiddleInitial>.<LastName>

For example the input string “Hun, Attila The” changes to:

<Attila><T>.<Hun>

• FirstName is displayed centered in a field 10 characters wide within angular

brackets

• MiddleInitial is displayed right-justified in a field 5 characters wide within

angular brackets followed by a dot

• LastName is displayed left-justified in a field 10 characters wide within angular

brackets

Be sure to test it with more than one input and not just "Hun, Attila The".

HINT:

1. Consider the following names as inputs: "Hun, Attila The" and "Newton, Sir

Isaac". You need to find the locations of the special characters that separate

the pieces. What separates the last name from the first name in each of the

strings? What separates the middle name from the first name in each of the

strings?

2. Write a search for each of those special characters using the index method in

the str class. That will give you the index of the separator between the last and

first names and also the index of the separator between the first and middle

names.

Sample Output 1:

Entername[LastName,FirstNameMiddleName]>Newton,SirIsaac

Thenewnameis:<Sir><I>.<Newton>

Sample Output 2:

Entername[LastName,FirstNameMiddleName]>Spencer,BrianJoe

Thenewnameis:<Brian><J>.<Spencer>

NOTE: You may assume that the user always enters a non-empty string in the

format specified in the question. You may also assume that first,middle and

last name will always be there. For example you don't need to test if middle

name is missing.

Q2 FizzBuzz is a children’s counting game where players count out starting at 1. For

numbers that are multiples of 3 , they say “Fizz”. For numbers that are multiples of 5

they say “Buzz”. For numbers that are multiples of both 3 and 5, they say

“FizzBuzz”. For all other numbers they say the number. Write a function called

play_fb that takes in a maximum number in its parameter and displays the

appropriate output as per the rules of the game. Here is the main function that you

need to include in your program:

defmain():

number=int(input('Entermaximumnumbertostartcounting>'))

play_fb(number)

main()

Sample Output


Q3 Write a program that reads in an unspecified number of integers separated by

spaces and finds the occurrences of each number.

Sample Output

Enternumbersseparatedbyspaces:123322213453

{'1':2,'3':4,'2':4,'5':1,'4':1}

1occurs2times

3occurs4times

2occurs4times

5occursonetime

4occursonetime

NOTE: You must use the dictionary to solve the problem. Also note that if the

number occurs only once then time not times is printed.

You can always assume that the user enters number separated by a single space

i.e no input validation is required.


站长地图