Dante
Dante

Reputation: 87

Python: Replace Numeric Value With String

I'm trying to replace a value entered by a user with a string to make the output cleaner

I thought an if statement would help, but I'm not sure how it would tie in with my intended output

 def main() :
    number = int(input("Enter your number: "))
    base = int(input("Convert to\n" \
    "   Binary[2] - Octal[8] - Hexadecimal[16]: "))

    if base == 2 :
        "binary"
     elif base == 8 :
        "octal"
    else:
        "hexadecimal"

    print("\n"+str(number) +" in "+ str(base) + " is: " + str(convert(number, 10, base)))


  def convert(fromNum, fromBase, toBase) :
    toNum = 0
    power = 0

    while fromNum > 0 :
        toNum += fromBase ** power * (fromNum % toBase)
        fromNum //= toBase
        power += 1
    return toNum

main()

What I'm trying to get: if user enters 5 as their number and 2 as conversion. Output would be: "5 in binary is: 101"

Upvotes: 1

Views: 1083

Answers (2)

Andrey Bulezyuk
Andrey Bulezyuk

Reputation: 197

Try

 def main() :
    number = int(input("Enter your number: "))
    base = int(input("Convert to\n" \
    "   Binary[2] - Octal[8] - Hexadecimal[16]: "))
    base_string = "None"        

    if base == 2 :
        base_string = "binary"
     elif base == 8 :
        base_string = "octal"
    else:
        base_string = "hexadecimal"

    print("\n {} in {} is: {}".format(str(number), base_string, str(convert(number, 10, base))))


  def convert(fromNum, fromBase, toBase) :
    toNum = 0
    power = 0

    while fromNum > 0 :
        toNum += fromBase ** power * (fromNum % toBase)
        fromNum //= toBase
        power += 1
    return toNum

main()

Your issue was the "binary" part in the if-statement. It has virtually no effect neither on your code nor on your output. You have to store the literal representation ("binary",...) in some variable ("base_string"). Then you can use this variable in your output.

Upvotes: 1

Bailey Parker
Bailey Parker

Reputation: 15905

As an aside, it looks like your base conversion won't actually do what you want. You should look at How to convert an integer in any base to a string? to do the conversion properly (hexadecimal has letters A-F in it, those aren't handled by your code, for example).

To accept a name instead of a number, you need to change this line of code:

base = int(input("Convert to\n   Binary[2] - Octal[8] - Hexadecimal[16]: "))

What's happening here? input() takes a line from stdin. In the interactive case, this means the user types something (hopefully a number) and then hits enter. We get that string. Then int converts that string to a number.

Your convert expects base to be a number. You want inputs like "binary" to correspond to base = 2. One way to achieve this is with a dict. It can map strings to numbers:

base_name_to_base = {'binary': 2, 'octal': 8, 'hexadecimal': 16}
base = base_name_to_base[input('Choose a base (binary, octal, hexadecimal): ')]

Note that base_name_to_base[x] can fail (raise KeyError) if x is not a key in the dict. So you want to handle this (what if the user enters "blah"?):

while True:
    try:
        base = base_name_to_base[input('Choose a base (binary, octal, hexadecimal): ')]
        break
    except KeyError:
        pass

This will loop until we hit the break (which only happens when indexing into base_name_to_base doesn't raise the key error.

Additionally, you may want to handle different cases (ex. "BINARY") or arbitrary whitespace (ex. " binary "). You can do this by calling .lower() and .strip() on the string returned by input().

Upvotes: 1

Related Questions