繁体   English   中英

检查3个单独的用户输入是否都与Python中列表列表的值匹配

[英]Checking if 3 separate User inputs all match the values of a list of lists in Python

我正在尝试创建一个非常简单的房地产数据库,该数据库将根据用户条件返回属性。

我有以下代码:

import sys

def main():


    print ("What type of property are you looking for?")

    Type = input()

    print ("How big of a property are you looking for?")

    Size = input()

    print ("Are you looking to lease or purchase a property?")

    Listing_Type = input()


    a = ('Retail', 1500, 'Lease')
    b = ('Industrial', 1440, 'Sale')
    c = ('Office', 1000, 'Lease')

    Database = (a, b, c)

    for i in Database:
        if Type == Database[i[0]] and Size == Database[i[1]] and Listing_Type == Database[i[2]]:

            text = "There is a %c square foot %d space for %z" % (Database[i[1]], Database[i[0]], Database[i[2]])

        else:

            i += 1

        if matches == 0:
            print ("Sorry, there were no matching properties.")


if __name__ == '__main__':
    main()

运行此命令时,我收到错误TypeError:元组索引必须是整数或切片,而不是str。

我仍在尝试了解其工作原理。 我需要检查属性类型和列表类型的字符串是否匹配,还要检查大小是否正确。

删除Database[] in Database[i[0]]in Database[i[1]]in Database[i[2]] ,您已经在Database上进行了迭代,因此循环中的i将是ab ,然后c 要访问这些元组的值, i[0]就足够了。

我首先在数据库中

i =('Retail', 1500, 'Lease')

i[0]='Retail'

所以

database[i[0]]=database['Retail']

但是数据库是元组而不是字典

换线

for i in database:

for i in range(len(database)):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM