1

So I am working on a conversion program where you have a current and a base and you should be able to convert your current and base to "decimal, octal etc". I basically have the program done, but I am stumped on how to implement the last method for trying to get user input for a new current or base that updates it to the new current and base.

Thank you all!

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()
Graham
  • 3,153
  • 3
  • 16
  • 31
unowho
  • 13
  • 4
  • 1
    why are you using double-underscore name-mangling? – juanpa.arrivillaga Nov 21 '18 at 17:14
  • To make it private instead of public – unowho Nov 21 '18 at 17:18
  • 1
    Python *doesn't have private and public*. To signal that a variable is not part of the public API, conventionally, a *single* underscore is added. – juanpa.arrivillaga Nov 21 '18 at 17:19
  • 1
    You should read [this](https://stackoverflow.com/a/7456865/8117067); you are using a non-standard method of name-mangling. – Graham Nov 21 '18 at 17:19
  • Okay, I will read that, but will that affect the user input from updating the current value and base value? – unowho Nov 21 '18 at 17:22
  • @unowho No, it won't really affect user input at all (besides the obvious difference of attribute names). It's really just stylistic, but learning good style now is good for avoiding pitfalls later. – Graham Nov 21 '18 at 17:25
  • Okay, thank you! I will use that for future programs. Do you have an idea of how to get the user input etc? – unowho Nov 21 '18 at 17:34
  • Working on an answer now, if someone doesn't beat me to it. – Graham Nov 21 '18 at 17:36
  • Thank you so much... I've also been working on an answer, but I just feel so stuck – unowho Nov 21 '18 at 17:38
  • Also make sure to read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Graham Nov 21 '18 at 18:24

1 Answers1

0

The main difficulty here seems to be input filtering. You should read this question; it provides good examples for how to generally filter input.

In your case, you can implement something like:

    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()

You'll also need to update self.menu(); I would add the string to this method, instead of keeping it as a separate method.

You should read PEP-8; it's the standard style guide for Python, and it can make your code easier for others to read and use. I would also consider posting your (finished) code on CodeReview.SE (make sure to read the guidelines for what's on-topic); there are numerous other improvements that can be made to your current code, and you will learn a lot.

Graham
  • 3,153
  • 3
  • 16
  • 31