(R1714) 95 : modify_book: Consider merging these comparisons with "in" to "field in ('pages', 'price')"

Viewed 763

My Code :

def modify_book():
        db = mysql.connector.connect(host='localhost',
                                     database='library',
                                     user='Aishwary_Pandey',
                                     password='aishwary')
        c = db.cursor()
        clear()
        print('M O D I F Y  --  B O O K  --  D E T A I L S')
        print('-'*120)
        print('\n1. Book Title')
        print('\n2. Book Author')
        print('\n3. Book Publisher')
        print('\n4. Book Pages')
        print('\n5. Book Price')
        print('\n6. Book Edition')
        print('\n7. Exit')
        print('\n\n')
        choice = int(input('Enter your choice : '))
        field = ''
        if choice == 1:
            field = 'title'
        if choice == 2:
            field = 'author'
        if choice == 3:
            field = 'publisher'
        if choice == 4:
            field = 'pages'
        if choice == 5:
            field = 'price'
        if choice == 6:
            field = 'edition'
    
        book_id = input('Enter ID of the Book : ')
        value = input('Enter the Value to be Updated : ')
        if field == 'pages' or field == 'price':
            sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
        else:
            sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'

error : basically there is no error but my idle (spyder) suggested me to merge these comparisons with "in" to "field in ('pages', 'price')" which I am not able to understand how would I do that.

please help.

1 Answers

This should "fix" it

if field in ('pages', 'price'):
    sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
else:
    sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'
Related