我想我的代码打印时,只问的人的姓名和他们在名单上的职位,如:
3 jake
5 Dan
而不是我的代码所做的:,,jake,,Dan,,,,
我该怎么做删除逗号和打印它的位置在列表中的每一行这里是我的代码到目前为止
while True:
print ('1 = Add Name ')
print ('2 = Display List ')
print ('3 = Quit \n')
choice = input('What would you like to do: ')
if choice== '1':
names = ['' for _ in range(9)]
name = input('Enter name: ')
position = input('What position in the list would you like to add to: ')
position = int(position)
if -1 < position < 9:
names.insert(position-1, name)
if choice == '2':
print(", ".join(names))
continue
if choice == '3':
print('Program Terminating')
break
else:
print('You have entered something invalid ')
print('There are only 10 names in the list')
continue
只需使用
enumerate
执行一个循环,并测试n
是否为空或者在一行中使用带有换行符的
str.join
内的生成器:相关问题 更多 >
编程相关推荐