首页 > 解决方案 > (R1714) 95: modify_book:考虑将这些比较与“in”合并到“field in ('pages', 'price')”

问题描述

我的代码:

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+';'

错误:基本上没有错误,但我的空闲(spyder)建议我将这些比较与“in”合并到“field in('pages','price')”,我无法理解我该怎么做。

请帮忙。

标签: python-3.xspyderanaconda3

解决方案


这应该“修复”它

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+';'

推荐阅读