繁体   English   中英

如何使用 MENU 创建 Python 十进制、二进制、八进制和十六进制转换器?

[英]How to create a Python Decimal, Binary, Octal and HexaDecimal Converter with MENU?

我对此很陌生,请耐心等待。 我正在尝试设计一个带有菜单的转换器,该菜单允许用户选择正在使用的数据类型(从和到)来转换(例如)二进制到十进制(反之亦然),我正在努力。 任何帮助都感激不尽。

到目前为止,当我运行程序时,代码会执行并显示菜单,询问我想从什么转换而来,但这大约是我目前能够掌握的。

这是我到目前为止所做的代码......

    # introductory menu
    print ("What are you converting from?");

def menu ():
    print ("\n1. Decimal")
    print ("2. Binary")
    print ("3. Octal")
    print ("4. Hexadecimal")
    print ("5. Exit")
    pick = int(input("Please enter an option: "))
    return pick

def toBinary (b):
    return bin(b)

def toDecimal (d):
    return int(d)

def toOctal (o):
    return oct(o)

def toHexadecimal (h):
    return hex(h)


def main():
    choice=menu()
    while choice !=5:
        if choice == 1:
            #convert Decimal to ?
            d = eval(input("What is your Decimal Value?: "))
            print(int(toDecimal (d)))

        elif choice == 2:
            #convert Binary to ?
            b = eval(input("What is your Binary Value?: "))
            print(str(toBinary (b)))

        elif choice == 3:
            #convert Octal to ?
            o = eval(input("What is your Octal Value?: "))
            print(str(toOctal (o)))

        elif choice == 4:
            #convert Hexadecimal to ?
            h = eval(input("What is your Hexadecimal Value?: "))
            print(str(toHexadecimal (h)))

        else:
            print ("Whoops!!! That is an Invalid Entry")

main()

这是程序运行的截图。

正如你所看到的,它没有做我真正想要它做的事情。

程序运行截图

您需要在 while 循环中包含您的 menu() 方法,否则选择值将始终相同并且永远不会从 2 更改(如您共享的屏幕截图)。

  choice=0
  while choice !=5:
        choice=menu()

此外,如果您希望您的转换程序正常工作,您需要添加另一种方法来选择您想要转换输入值的方式:

def transform_to(value):
    print("Transform to ")
    choice = menu()
    if choice == 1:
        print(int(toDecimal (value)))
        choice=5

    elif choice == 2:
        print(str(toBinary (value)))
        choice=5


    elif choice == 3:
        print(str(toOctal (value)))
        choice=5

    elif choice == 4:
        print(str(toHexadecimal (value)))
        choice=5

所以你的最终脚本是:

# introductory menu
print ("What are you converting from?");

def menu ():
    print ("\n1. Decimal")
    print ("2. Binary")
    print ("3. Octal")
    print ("4. Hexadecimal")
    print ("5. Exit")
    pick = int(input("Please enter an option: "))
    return pick

def toBinary (b):
    return bin(b)

def toDecimal (d):
    return int(d)

def toOctal (o):
    return oct(o)

def toHexadecimal (h):
    return hex(h)


def transform_to(value):
    print("Transform to ")
    choice = menu()
    if choice == 1:
        print(int(toDecimal (value)))
        choice=5

    elif choice == 2:
        print(str(toBinary (value)))
        choice=5


    elif choice == 3:
        print(str(toOctal (value)))
        choice=5

    elif choice == 4:
        print(str(toHexadecimal (value)))
        choice=5

def main():
    choice=0
    while choice !=5:
        choice=menu()
        if choice == 1:
            #convert Decimal to ?
            d = eval(input("What is your Decimal Value?: "))
            transform_to(d)
            print(int(toDecimal (d)))

        elif choice == 2:
            #convert Binary to ?
            b = eval(input("What is your Binary Value?: "))
            transform_to(b)


        elif choice == 3:
            #convert Octal to ?
            o = eval(input("What is your Octal Value?: "))
            transform_to(o)


        elif choice == 4:
            #convert Hexadecimal to ?
            h = eval(input("What is your Hexadecimal Value?: "))
            transform_to(h)

main()

暂无
暂无

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

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