11.21练习
作者:互联网
鸽了那么多久我终于学python了!!!
题目①描述:
It is oftentimes advantageous to be able to transfer data between multiple lists while rearranging their order. For instance, say that list1 = [1,2,3,4,5,6,7,8,9] and you wish to add the numbers in the index range 4:7 of list1 to another list, list2, in reverse order while simultaneously removing them from list1. If list2 = [100,200], the result will be list2 = [100,200,7,6,5]. Write a function named transform that takes as arguments list1, list2, r1, and r2, that removes items from list1 in the slice r1:r2, appends them onto list2 in reverse order, and returns the resulting list. For example, in this case, the function call will be as the following:
transform(list1, list2, 4,7).
请按功能描述实现transform函数,并设计合适的主程序调用该函数。
def zhuanhuan(list):
for i in range(len(list)):
list[i] = int(list[i])
return list
def transform(list1,list2,r1,r2):
listnew = list2 + list(reversed(list1[r1:r2]))
return listnew
list11 = input('请输入list1的值,并用逗号分隔:').split(',')
list22 = input('请输入list2的值,并用逗号分隔:').split(',')
r11 = input('请输入r1的值:')
r22 = input('请输入r2的值:')
r111 = int(r11)
r222 = int(r22)
list111 = zhuanhuan(list11)
list222 = zhuanhuan(list22)
print(transform(list111,list222,r111,r222))
题目②描述:
Write the program that prompts the user for a list of
numbers and prints out the maximum and minimum of the numbers at
the end when the user enters “done”. Write the program to store the
numbers the user enters in a list and use the max() and min() functions to
compute the maximum and minimum numbers after the loop completes.
Enter a number: 6
Enter a number: 2
Enter a number: 9
Enter a number: 3
Enter a number: 5
Enter a number: done
Maximum: 9.0
Minimum: 2.0
list = []
while True:
a = input("Enter a number,to terminate,enter done:")
if a == "done":
break
else:
list.append(a)
def zhuanhuan(list1):
for i in range(len(list1)):
list1[i] = float(list1[i])
return list1
print('Maximum:',max(zhuanhuan(list)))
print('Minimum:',min(zhuanhuan(list)))
只符合了题目的基本要求(循环结构懒得加了,可能还有一些BUG),也没啥创新的地方,总之差距还很大,任重而道远!!!
看电影去辽。_(:3」∠)_
标签:zhuanhuan,list,11.21,练习,list1,number,list2,Enter 来源: https://blog.csdn.net/qq_63174393/article/details/121450492