逻辑任务:第一个程序无法正常工作

问题描述 投票:0回答:1

今天我尝试完成这个任务: 简而言之,任务是获取给定的字符串,其中附加有单词“meter”的各种前缀,并且输出是所有前缀与单词组合时的 10^x 次方。 Task

最后我写下我的答案

def main() -> None:
    """
    tera    10^12
    giga    10^9
    mega    10^6
    kilo    10^3
    deci    10^-1
    centi   10^-2
    milli   10^-3
    micro   10^-6
    nano    10^-9
    """
    num_dict = {'tera': '12',
                'giga': '9',
                'mega': '6',
                'kilo': '3',
                'deci': '-1',
                'centi': '-2',
                'milli': '-3',
                'micro': '-6',
                'nano': '-9'}

    num_text: str = input()
    if num_text == "meter":
        print(0)
    else:
        num_text = num_text.replace("meter", "")

        for key, value in num_dict.items():
            num_text = num_text.replace(key, f"+{value}")

        num_text = ("(" if "^" in num_text else "") + num_text[(1 if num_text[0] == "+" else 0):]
        num_text = num_text.replace("^", ")*")

        result = eval(num_text)
        print(result)


if __name__ == "__main__":
    """
    For test:
    meter
    kilometer
    megananokilogigamicrometer
    """
    main()

但是测试显示80分(满分100分),所以我决定再写一个代码,这里是:

def main() -> None:
    temp: str = input()
    counter: int = 0
    num_dict = {'tera': '12',
                'giga': '9',
                'mega': '6',
                'kilo': '3',
                'deci': '-1',
                'centi': '-2',
                'milli': '-3',
                'micro': '-6',
                'nano': '-9'}
    for name, value in num_dict.items():
        counter += temp.count(name) * int(value)
    if "^" in temp:
        counter *= int(temp.split("^")[1])
    print(counter)


if __name__ == "__main__":
    """
    meter
    kilometer
    megananokilogigamicrometer
    """
    main()

得分100分。 但我很好奇为什么我的第一个代码不能完全正确工作。

python python-3.x logic
1个回答
0
投票

您的第一个代码在

meter^2
meter^3
的情况下无法提供正确的答案,正如您从以下代码的输出中看到的:

# The task is to take a given string with various prefixes attached to the word "meter" 
# and output is the power of 10^x when all the prefixes are combined with the word.
# My first answer was: 
def main1() -> None:
    global temp, num_dict
    #num_text: str = input()
    num_text = temp
    if num_text == "meter":
        print(0)
    else:
        num_text = num_text.replace("meter", "")
        for key, value in num_dict.items():
            num_text = num_text.replace(key, f"+{value}")
        num_text = ("(" if "^" in num_text else "") + num_text[(1 if num_text[0] == "+" else 0):]
        num_text = num_text.replace("^", ")*")
        print( eval(num_text) )

#But the test showed 80 out of 100 points, so I decided to write another code, here it is:
def main2() -> None:
    global temp, num_dict
    #temp: str = input()
    counter: int = 0
    for name, value in num_dict.items():
        counter += temp.count(name) * int(value)
    if "^" in temp:
        counter *= int(temp.split("^")[1])
    print(counter)
#   Now it scored 100 points.
#   But I'm curious why my first code isn't working completely correctly.
Answer="""\
    Your first code does not correctly cover the cases of 'meter^2 and meter^3
        See the comparison of the output of code 1 compared to code 2: """
mlnStrInputData = """\
meter^2
meter^3"""
num_dict={'tera':'12','giga':'9','mega':'6', 'kilo':'3','deci': '-1','centi': '-2','milli': '-3','micro':'-6','nano': '-9'}
if __name__ == "__main__":
    for temp in mlnStrInputData.split("\n"): 
        print("main1:", temp, end=" -> ")
        main1()
        print("main2:", temp, end=" -> ")
        main2()

哪个输出:

main1: meter^2 -> ()
main2: meter^2 -> 0
main1: meter^3 -> ()
main2: meter^3 -> 0

无法输出整数值,而是打印括号。

© www.soinside.com 2019 - 2024. All rights reserved.