![]() ![]() Normally it should be ff, or at least (15, 15) or something. Similar to the previous point, this can be shortened: n = abs(n)īasically this program can convert any number from any base to another base including negative bases as long as the input is coherent.įor input arguments 255 10 16 it gives me: You entered 255 in base 10. You could write in a shorter, more natural way: number *= base ** i * -1 ![]() Since it's a list, making the name plural would be more natural. Maybe "string" or "numString" would be better. I'd find it more clear: if n.startswith('-'):Īt this point it's also clear that n is not a good name. Notice that I used reversed(.) instead of number, Instead of declaring an empty number = and then appending elements, Print("Your number in base " + str(baseOut) + " is " + str(convertToAnyBase(n, baseOut)) + ".")Ī couple of simplifications are possible that will make this code more Pythonic. Print("Your number in base " + str(baseOut) + " is " + str(n) + ".") If abs(baseIn) = 10 and abs(baseOut) = 10: # If both bases are ten then we have already calculated its value. Print("Your number in base " + str(baseOut) + " is " + str('-' + ones) + ".") Print("Your number in base " + str(baseOut) + " is " + str(ones) + ".") Print("You entered " + x + " in base " + str(baseIn) + ".") # Add the absolute value of the base to the remainder and add one to n. # If the base is negative, remainder will be a negative number. # After we get the correct n, we can now negate it. # returns -3.0 which is one off the actual value (we actually want -2 in this example). # then we have to take the absolute value of n before dividing it. # Special case: If n is less than zero and base is greater than zero. Python str () is a built-in function, which is used to convert integer (int) to string. # then replace the minus sign with a blank character to correctly convert string to int. There are two python built-in methods, which are commonly used to convert string to int and int to string python as shwon below: Python int () is a built-in function, which is used to convert the string to an integer value. # If n is a negative number (it's a string at this moment of the code). The base you'd like your output to be in.The base the number you just inputted is in.The program is run by giving it 3 inputs: that's nonsense because you can't have a negative number if you're in a negative base, if I understand correctly. Did I follow the Python 3 standard or commit any obvious mistakes? I am also wondering if I can efficiently filter out nonsensical input without making explicit cases for each one? Basically this program can convert any number from any base to another base including negative bases as long as the input is coherent. I coded this program as a challenge for /r/dailyprogrammer. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |