简体   繁体   English

如何提高程序效率?

[英]How can I make my program more efficient?

I successfully made a program that converts binary to decimal. 我成功制作了一个将二进制转换为十进制的程序。 I have a strong feeling this could be done way simpler and more efficiently than the way I created it. 我强烈感觉这可以比我创建它的方式更简单,更有效地完成。 This is what I have: 这就是我所拥有的:

   #Binary to Decimal Conversion
#Input Data and assign variables
    def main():
    binary = input("Enter rhe binary number you'd like to convert: ")
    x = "1"
    y = "0"
    num1 = 0
    num2 = 0
    num3 = 0
    num4 = 0
    num5 = 0
    num6 = 0
    num7 = 0
    num8 = 0

    #Error Handling for incorrect number of characters 
    while len(binary)!= 8:
        print("Please enter a valid value")
        binary = input("Enter rhe binary number you'd like to convert: ")
        if binary == 8:
            break

    #Processing the data and conversion
    #Checks to see if character 1 in the string binary is equal to "1" or "0"
        if binary[0] == "1":
            num1 = 128
        elif binary[0] == "0":
            num1 = 0
        else:
            print('Error, value 1 is not = to "1" or "0"')
            main()

        if binary[1] == "1":
            num2 = 64
        elif binary[1]:
            num2 = 0
        else:
            print('Error, value 2 is not = to "1" or "0"')
            main()


        if binary[2] == "1":
            num3 = 32
        elif binary[2] == "0":
            num3 = 0
        else:
            print('Error, value 3 is not = to "1" or "0"')
            main()

        if binary[3] == "1":
            num4 = 16
        elif binary[3] == "0":
            num4 = 0
        else:
            print('Error, value 4 is not = to "1" or "0"')
            main()


        if binary[4] == "1":
            num5 = 8
        elif binary[4] == "0":
            num5 = 0
        else:
            print('Error, value 5 is not = to "1" or "0"')
            main()


        if binary[5] == "1":
            num6 = 4
        elif binary[5] == "0":
            num6 = 0
        else:
            print('Error, value 1 is not = to "1" or "0"')
            main()


        if binary[6] == "1":
            num7 = 2
        elif binary[6] == "0":
            num7 = 0
        else:
            print('Error, value 1 is not = to "1" or "0"')
            main()


        if binary[7] == "1":
            num8 = 1
        elif binary[7] == "0":
            num8 = 0
        else:
            print('Error, value 1 is not = to "1" or "0"')
            main()


    #output and rerun
        print("The converted number is: ", num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8)
        choice = ' ' 
        while choice != "1" and choice != "2":
            choice = input("Would you like to perform another coversion?:\n[1]Yes\n[2]Naw\n")
        if choice == "1":
            main()
        elif choice == "2":
            exit()
        else:
            print("Error please select [1] or [2]")

    main()

I was thinking it would be possible to run the actual conversion part using a loop? 我以为有可能使用循环来运行实际的转换部分? Also, I am fairly new to python... yes I did google it first. 此外,我对python还是相当陌生的。是的,我首先在Google上进行了搜索。 Also, all of the stuff under main() IS indented, I had troubles formatting my code on stackoverflow. 而且,main()下的所有内容都是缩进的,我在用stackoverflow格式化代码时遇到了麻烦。

Functional programming is coming. 功能编程即将到来。 Beware! 谨防!

from functools import reduce

source = input("Enter the binary number you'd like to convert: ")
binary = map(int, list(source))
print(reduce(lambda a,b: 2*a+b, binary, 0))

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

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