python-3.x - (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')”,我无法理解我该怎么做。
请帮忙。
解决方案
这应该“修复”它
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+';'
推荐阅读
- tinkercad - 运行此代码时我得到了 14:29。尝试使用 Ardouino 电路
- visual-c++ - MFC:默认情况下使用什么方法将工具栏按钮设置为全部工具栏按钮的子集?
- android - 使用 Rxjava kotlin 类初始化屏幕后,拳头按钮单击丢失
- javascript - 等待 forEach 循环结束渲染 - Javascript
- python - 如何使用异步 python 测试网络带宽和延迟?
- appium - 尝试使用 Appium Server 对 Windows 应用程序运行机器人框架测试用例 - 可能吗?
- python-3.x - Sympy欧拉恒等式两个方程
- javascript - 用循环反应 setTimeout
- google-cloud-dataflow - Apache Beam 记录带有错误标签的消息
- numpy - 使用 anaconda 在 ubuntu 上安装带有 MKL 的 numpy 1.19?