【发布时间】:2017-05-20 07:47:55
【问题描述】:
我正在创建一个颜色数据库。对于这个数据库,我希望用户能够添加自己的颜色,如果可能的话。最初,我是使用 list.append 完成的。我会让用户通过输入阴影颜色及其 RGB 值来添加颜色。然后将这些数据传递给我将其添加到数据库的函数。我现在正试图通过确保 RGB 值介于 0 和 255 之间来改变这一点。如果数字不在 0 和 255 之间,它应该打印出该值不正确,他们必须添加一个新数字。这是我到目前为止所拥有的:
def addColour():
values = []
values.append (input("What shade is the colour?\n"))
red = values.append(int(input("What is its red value?\n")))
while red > 255 or red < 0:
print("That is an incorrect figure. It must be between 255 and 0")
values.append(input("What is its green value?\n"))
values.append(input("What is its blue value?\n"))
insertData(values)
然后我将用户的输入添加到数据库中:(仅供参考)
def insertData(values):
with sqlite3.connect("colours.db") as db:
cursor = db.cursor()
sql = "insert into Colours (Shade, Red, Green, Blue) values (?, ?, ?, ?)"
cursor.execute(sql,values)
db.commit()
question = input("Do you want to add another colour?")
if question.lower() == "y":
addColour()
else:
mainMenu()
我得到的类型错误也列在下面:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
【问题讨论】:
-
list.append返回None。另外,如果你的代码真的进入了它,你就会有一个无限循环。 -
我将如何在用户可以输入的内容上实现整数边界?
标签: list python-3.x integer