我如何根据参数和参数搜索对象?

2024-09-26 18:19:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个搜索算法的帮助,搜索海报根据其大小,颜色和不同的参数

print("Hello!, welcome to my Poster shop")


class Posters():
    """make a template for the posters"""

    def __init__(self, size, name, colors, category):
        """Gives the poster some categories"""
        self.size = size
        self.name = name
        self.colors = colors
        self.category = category


poster1 = Posters(12, 'global_warming', 'blue', 'environment')
poster2 = Posters(11, 'volcano erruption', 'red', 'natural')

sizes = {poster1: poster1.size, poster2: poster2.size}

size_input = input("Search for different sized posters here: ")

for key, value in sizes.items():
    if size_input == value:
        print(sizes[value])

它没有显示结果,只是空白


Tags: thenameselfforinputsizevalueprint
1条回答
网友
1楼 · 发布于 2024-09-26 18:19:09

input函数返回一个string^{<Poster的cd3>}属性是int。所以当你做if size_input == value:的时候,你是在比较stringint,这会给你False

您需要将中的用户输入转换为int。用int函数包装输入函数

size_input = int(input("Search for different sized posters here: "))

相关问题 更多 >

    热门问题