Answer to Question #296013 in Python for Michael

Question #296013

While taking a viva in class, the teacher decides to take the viva of two roll numbers at a time. For this she calls out the roll numbers from the list of roll numbers as: One roll number from the first nth roll number and Second roll number from the nth last roll number. For example if the numbers are: rollnum=[21, 24, 45, 6, 7, 23, 9, 11, 15, 18], and n=4, then :One roll number called for viva is 6 and second roll number called for viva is 9.

You need to write a python function which take a list A and nth roll number as input and return first nth roll number and Second roll number from the nth last roll number as shown in example. If nth roll number is not lies in the range of list, then function will return "n is not in range" in place of first roll number and empty string (“”) in place of second roll number.

 

Example-1

Example-2

Input:

[21, 24, 45, 6, 7, 23, 9, 11, 15, 18]

20

 

Output:

n is not in range

Input:

[21, 24, 45, 6, 7, 23, 9, 11, 15, 18]

5

 

Output:

7

23



1
Expert's answer
2022-02-10T09:49:18-0500
def roll_nums(lst, n):
	if n > len(lst) or n < 1:
		return "n is not in range", ""
	else:
		return lst[n-1], lst[-n]


rollnum = [21, 24, 45, 6, 7, 23, 9, 11, 15, 18]
n=5
print(*roll_nums(rollnum, n),sep="\n")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS