将用户输入添加到基本转换器类

时间:2018-11-21 17:07:26

标签: python python-3.x python-idle

因此,我正在开发一个转换程序,其中有一个电流和一个基数,并且应该能够将电流和基数转换为“十进制,八进制等”。我基本上已经完成了程序,但是我对如何实现最后一种方法来尝试获取新的电流或基准的用户输入感到困惑,这将其更新为新的电流和基准。

谢谢大家!

class numberConverter:
    def __init__(self, current=0, base=10):
        self.__current = current
        self.__base = base
    def print(self):
        print("Current set to:",
              self.__current, "Base set to:", self.__base)
    def menu(self):
        return("\n'S'et\n'C'urrent\n'B'inary\n'H'ex\n'O'ctal\n'D'ecimal\n'q'uit: ")
#Converts to Hexidecimal 
    def convHex(self):
        print("Hex Conversion Commencing")
        print("Converting:", self.__current, "Base", self.__base, "to Base 16")
        self.__current = format(int(str(self.__current), self.__base), '02X')
        self.__base = 16
        self.print()
#Converts to Octal
    def convOct(self):
        print("Octal Conversion Commencing")
        print("Converting:", self.__current, "Base", self.__base, "to Base 8")
        self.__current = format(int(str(self.__current), self.__base), '02o')
        self.__base = 8
        self.print()
#Converts to Decimal 
    def convDec(self):
        print("Decimal Conversion Commencing")
        print("Converting:", self.__current, "Base", self.__base, "to Base 10")
        self.__current = format(int(str(self.__current), self.__base))
        self.__base = 10
        self.print()
#Converts to Binary 
    def convBinary(self):
        print("Binary Conversion Commencing")
        print("Converting:", self.__current, "Base", self.__base, "to Base 2")
        self.__current = format(int(str(self.__current), self.__base), '02b')
        self.__base = 2
        self.print()
#Change the current value or base
#This is the section I am trying to work on
    def setCurrent(self):
        userInput = str(input("Set 'C'urrent or 'B'ase: "))
        if userInput.upper() == "C":
            print(input("Enter new current value: "))
        elif userInput.upper() == "B":
            print(input("Enter new base value: "))

        num1 = numberConverter(14,10)
        select = ""
        while select !='q':
            select = input(num1.menu())
            if select == "C":
                num1.print()
            if select == "H":
                num1.convHex()
            if select == "O":
                num1.convOct()
            if select == "D":
                num1.convDec()
            if select == "B":
                num1.convBinary()
            if select == "S":
                num1.setCurrent()

1 个答案:

答案 0 :(得分:0)

这里的主要困难似乎是输入过滤。您应该阅读this question;它提供了有关如何一般过滤输入的良好示例。

在您的情况下,您可以执行以下操作:

    def setCurrent(self):
        userInput = str(input("Set 'C'urrent or 'B'ase: "))
        if userInput.upper() == "C":
            while True:
                try:
                    value = int(input("Enter new current value: "))
                    break
                except ValueError:
                    print("Invalid number!")
            self.__current = value
        elif userInput.upper() == "B":
            while True:
                base = input(self.menu())
                if base in "HODB":
                    break
                else:
                    print('Base must be "H", "O", "D", or "B"!')
            if base == "H":
                self.convHex()
            elif base == "O":
                self.convOct()
            elif base == "D":
                self.convDec()
            elif base == "B":
                self.convBinary()

您还需要更新self.menu();我会将字符串添加到此方法中,而不是将其保留为单独的方法。

您应该阅读PEP-8;这是Python的标准样式指南,它可以使您的代码更易于他人阅读和使用。我还将考虑在CodeReview.SE上发布您的(完成的)代码(请务必阅读the guidelines for what's on-topic);您当前的代码可以进行许多其他改进,您将学到很多东西。