Я новичок в python и пытаюсь выполнить код задания, но столкнулся с некоторыми проблемами.Передача аргумента в Python
Что я пытался сделать до сих пор: import
файл в программу, return
это main()
.
Я создал другой вызов функции print_names()
, чтобы напечатать имена в списке по одному в строке и с одним интервалом, и я попытался сортировать содержимое из файла в алфавитном порядке (меня попросили выполнить сортировку в main()
).
Тогда теперь я хочу передать этот отсортированный список в другую функцию, чтобы я мог поместить их в новый выходной файл.
def main():
names_in() # This function import the file and read all the content and put the content into a list.
print_names(names_in) # Before the names are sorted.
names_sorted = sorted(names_in()) # Sort the list of names.
for item in names_sorted:
print(item)
def names_in():
infile = open('names.txt','r')
names_list = [] # empty list.
names = infile.readline() # read contents.
# loop for continue to read.
while names != '':
names = infile.readline() # continue to the next name.
names = names.rstrip('\n') # return a copy of the string which all \n has been stripped from the end of the string.
names_list.append(names) # write names in the file into a list.
return names_list # return the list back to the function.
infile.close() # close the file.
def print_names(names_in): # This function will print out the names in the list one per line, single-spaced.
for item in names_in():
print(item)
main()
И ... что это за проблема? – felipsmartins