简体   繁体   English

我怎样才能使这个构造更简单,就像在 python 中使用类或函数一样?

[英]How can I make this construction simpler, like with classes or functions in python?

print(
    "1. Engine block \n"
    "2. Pistons \n"
    "3. Crankshaft \n"
    "4. Camshaft \n"
    "5. Cylinder head \n"
    "6. Connecting Rod \n"
)

part = int(input("Choose an engine part to show you details about it: "))

if part == 1:
    print("Engine block")
elif part == 2:
    print("Pistons")
elif part == 3:
    print("Crankshaft")

I have 26 statements like these and I wonder if I can make it simpler with classes,我有 26 个这样的语句,我想知道我是否可以通过类简化它,
functions, lists or something I can't find a way??功能,列表或我找不到方法的东西?

You can create a class with Question and answer.您可以创建一个带有问答的类。 Also add function in the class to check if the input is same as the Answer and return the result accordingly.还要在类中添加函数以检查输入是否与答案相同并相应地返回结果。 You can use the regular expression to fetch the value of right answer from the Question text.您可以使用正则表达式从问题文本中获取正确答案的值。

I would suggest using something like this:我建议使用这样的东西:

def info_about(parts):
    info = list(parts.values())
    print("\n".join(f"{i+1}.\t\t{name}" for i, name in enumerate(parts)))

    print()
    while True:
        part = input("Choose an engine part to show you details about it: ")
        if not part.isnumeric():
            print("Please enter a valid number")
            continue

        part = int(part)
        if part > len(info) or part < 1:
            print("Please enter a valid number")
            continue
        break

    print(info[part-1])

parts = {
    "Engine block":         "Info about engine block",
    "Pistons":              "Info about pistons",
    "Crankshaft":           "Info about crankshaft",
    "Camshaft":             "Info about camshaft",
    "Cylinder head":        "Info about cylinder head",
    "Connecting Rod":       "Info about connecting rod",
}

info_about(parts)

This now checks the input to ensure that it is valid - that it's numeric, and that it's between 1 and the number of parts.现在检查输入以确保它是有效的——它是数字,并且它在 1 和零件数之间。

print(
    "1. Engine block \n"
    "2. Pistons \n"
    "3. Crankshaft \n"
    "4. Camshaft \n"
    "5. Cylinder head \n"
    "6. Connecting Rod \n"
)

part = int(input("Choose an engine part to show you details about it: "))

if part == 1:
    print("Engine block")
elif part == 2:
    print("Pistons")
elif part == 3:
    print("Crankshaft")

I have 26 statements like these and I wonder if I can make it simpler with classes,我有26条这样的语句,我想知道是否可以通过类使它更简单,
functions, lists or something I can't find a way??函数,列表或我找不到方法的东西?

If you really want an extra function如果你真的想要一个额外的功能

print(
    "1. Engine block\n"
    "2. Pistons\n"
    "3. Crankshaft\n"
    "4. Camshaft\n"
    "5. Cylinder head\n"
    "6. Connecting Rod\n"
)

def list_dict():
    return {1: "Engine block", 2: "Pistons", 3: "Crankshaft"}

def ans(part):
    return list_dict()[part]


part = int(input("Choose an engine part to show you details about it: "))
print(ans(part))

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

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