我正在编写一个程序,我在其中获取用户输入以填充两个列表,每个列表包含3个整数(总共6个)并打印两个已排序的列表并合并它。如何让我的程序在列表之间切换并向用户询问整数?这就是它应该如何工作:
Please enter a value for listone:
Please enter a value for listtwo: # instead I have listone displayed 3 times
Please enter a value for listone:
Please enter a value for listtwo:
到目前为止,这是我的代码:
def user_input():
listone = []
listtwo = []
listone_value = int(input("Please enter a value for listone: "))
for i in range (2):
listone_value = int(input("Please enter a value for listone: "))
listtwo_value = int(input("Please enter a value for listtwo: "))
for i in range(2):
listtwo_value = int(input("Please enter a value for listtwo: "))
def merge_lists(listone, listtwo):
list = []
while listone and listtwo:
L.append( listone.pop(0) if listone[0] < listtwo[0] else listtwo.pop(0) )
L.extend(listone if listone else listtwo)
return list
我只是不知道如何让问题交替进行,因为我还是比较新的程序。
答案 0 :(得分:3)
如果你确切知道你想要多少输入,只需使用for循环
def user_input():
list_one = []
list_two = []
for i in range(3):
list_one.append(int(input('Please enter a value for listone: ')))
list_two.append(int(input('Please enter a value for listtwo: ')))
return list_one, list_two