开发者

Im trying to make a program in which the user will enter a list of books/movies, with the title, author and year created, but it keeps repeating

The program I have written does not work as intended, I have spent quite a while trying to solve it and cannot figure it out, it keeps repeating over and over, or until the end of the loop, any help would be much appreciated. heres the code

def find_similar(choice1, list1):
    if choice1 == 'author':
        name = str(input('enter name of which author you would like to find: '))
        i = 0
        while(i<len(list1)):
            if name == list1[i]:
                print(list1[i])
                print(list1[i-1])
                print(list1[i+1])
                i= i+1
            else:
                i= i+1
        else:
            print('index too low')
        
              
    if choice1 =='year':
        index1 = int(input('enter index of which year you would like to sort by: '))
        if(index1-1>=0):
            i = 0
            print(list1[index1])
            print(list1[index1-1])
            print(list1[index1-2])
            while(i<=len(list1)):
                if index1 == i:
                    print(list1[i])
                    print(list1[i-1])
                    print(list1[i-2])
                    i= i+1
                else:
                    i = i +1
        else:
            print('index too low')            
user_info = input('Enter a series of books, stories or movies, their year, and author/director, each of which followed by a comma (example: Tell-Tale Heart Poe 1843,)(type ! to end): ')
user_response_list = 开发者_运维知识库user_info.split()
print(user_response_list)
choice = input('What would you like to search for? (author or year)')
find_similar(choice, user_response_list)
        
        
        

I've tried using a loop to go through the list in order to find anything matching, but it just keeps repeating.


This might be a good time to use for loops and enumerate().

Also f-strings in python are your best friend. Google them and learn how to use them if you do not already (example includes f-strings)

def find_similar(choice, list1):
    ## do not need seperate variables to do a simple == check
    if choice == "author":
        search = input('enter name')
    elif choice == "year":
        search = int(input('enter year'))
    else:
        print(f'{choice} is not a valid choice') ## search python f-strings
        return ##exits the function and returns None
    for i, item in enumerate(list1): ## enumerate(somelist) returns an iterator similar to i = i+1 or i+=1 and the next item in the list.
        if search == item:
            if choice=="author":
                print(f'{list1[i-1]}, {item}, {list1[1+1]}')
                return ##exits the function and the loop
            elif choice=="author":
                print(f'{list1[i-2]}, {list1[i-1]}, {item}')
                return ##exits the function and the loop
    print('finished')

Hopefully that will help out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜