Adding strings (as representation for base 12 numbers) and integrers/ other strings in python
python convert string to double
python int to string format
invalid literal for int() with base 10
python string to float
python 2.7 string to int
python convert string to int error
python return integer from string
I need to create a calculator that can add numbers in base 12 and with different limits at the differents diggits.
Base 12 sequence: [0,1,2,3,4,5,6,7,8,9,"A","B"]
The limits must be:
- First digit: limit "B"
- Second digit: limit 4
That means you would count like this:[1,2,3,4,5,6,7,8,9,A,B,10,11,...48,49,4A,4B]
but I don´t know how could I make it so that I can sum 2 numbers
I have the following code in python:
list1=[0,1,2,3,4,5,6,7,8,9,"A","B"] list2=[0,1,2,3,4] list3=[0,1,2,3,4,5,6,7,8,9,"A","B"] list4=[0,1,2,3,4] def calculadora (entrada1, operacion, entrada2): #parte de valor 1: digito1_1=str(list2[int(entrada1//12)]) digito1_2=str(list1[int(entrada1%12)]) valor1=float(digito1_1+digito1_2) #parte de valor 2 digito2_1=str(list2[int(entrada2//12)]) digito2_2=str(list1[int(entrada2%12)]) valor2=float(digito2_1+digito2_2) if operacion==str("suma") or "+": return float(valor1+valor2) entrada1 = float(input("inserte primer valor")) operacion=str(input("inserte operación")) entrada2 = float(input("inserte segundo valor")) print (calculadora(entrada1,operacion,entrada2))
It works for numbers but wenn I want to sum numbers like 3B, it gives me a ValueError as it is coded as string.
Could someone help me or say me how could I do it to sum such numbers please?
The easiest way to convert a base-12 encoded string to int is via
int(string, 12). The reverse is not quite as easy, because Python doesn't seem to have a built-in way to do that. You can use format specifiers for binary, octal, and hex, but not arbitrary bases.
You can get a reversed list of digits using
divmod(), which does division with remainder.
def to_digits(x, base): while x > 0: x, rem = divmod(x, base) yield rem
But to round-trip, we want a string. Convert the int to a character (using a string as a lookup table), and join them into a string, then use a negatively-stepped slice to reverse it.
def to_base_12(num): return ''.join('0123456789AB'[d] for d in to_digits(num, 12))[::-1]
With a longer lookup table, you could generalize this into higher bases.
Strings are already sequences, but if you want to convert that back to a list, you can just call
list() on it. The inverse is that
''.join() method you just saw.
Now that you can convert your base-12 representation to Python int objects and back, you can simply add them with
5.5. Converting an Integer to a String in Any Base, For example, convert the integer 10 to its string representation in decimal as "10" Let's look at a concrete example using base 10 and the number 769. Concatenate the single-digit strings together to form the final result. ActiveCode 1 shows the Python code that implements the algorithm outlined above for any base Introduction Python allows you to convert strings, integers, and floats interchangeably in a few different ways. The simplest way to do this is using the basic str(), int(), and float() functions. On top of this, there are a couple of other ways as well. Before we get in to converting strings to numbers, and converting numbers to strings, let's first see a bit about how strings and numbers are
Here is slight variation on the excellent answer from gilch that handles negative numbers and zero.
def convert_to_string(num, base): ''' Convert integer (num) to base less than base 37. ''' alpha = string.digits + string.ascii_lowercase assert isinstance(num, int) assert isinstance(base, int) assert 1 < base <= len(alpha) if num == 0: return '0' def to_digits(num, base, alpha): ''' Generator to convert digits in reverse order. ''' while num > 0: num, rem = divmod(num, base) yield alpha[rem] sign = '' if num < 0: num, sign = -num, '-' return sign + ''.join(d for d in reversed(tuple(to_digits(num, base, alpha)))) def convert_from_string(num, base): ''' Convert string in base X to integer. ''' return int(str(num), base) def test(): ''' Test conversions. ''' assert convert_to_string(0, 2) == '0' assert convert_to_string(4, 2) == '100' assert convert_to_string(23, 12) == '1b' assert convert_to_string(-6, 2) == '-110' assert convert_from_string('1b', 12) == 23 assert convert_from_string('-110', 2) == -6
How to Convert a Python String to int – Real Python, In this tutorial, you'll learn how you can convert a Python string to an int . You can also represent your integers with other number systems in Python in <module> ValueError: invalid literal for int() with base 10: '0x12F'. x - String or number to be converted to an integer. base - It represents the numeral system of the first argument. Its value can be 0 and 2–36. If no base is given, the default is 10 (decimal integer). Usually, integers are expressed in hexadecimal (base 16), decimal (base 10), octal (base 8), or binary (base 2) notation.
You can add them directly if you convert the A and B to 11 and 12. Then as each number is now a list of digits one can add them the same way that an ALU does. See the section on addition of integers in any text on computer arithmetic.
def add(A, B): result =  carry = 0 for a, b in reversed(zip(A,B)): carry, digit = divmod(a + b + carry, 12) result.append(digit) if carry: result.append(carry) result.reverse() return result >>> add([4,3],[6,11]) [11, 2] >>> add([5,3],[6,11]) [1, 0, 2]
The lists are MSD first. The double reversing is not needed if the lists were in LSD first.
How to Convert Strings into Integers in Python, In Python you can convert a string to integer using the build-in int() function. Usually, integers are expressed in hexadecimal (base 16), decimal (base in <module> TypeError: cannot concatenate 'str' and 'int' objects When converting strings that represent integers in different number Dec 12, 2019 Under some circumstances, a floating-point operation can result in a value that is essentially infinite. The string representation of such a number in Python is 'inf'. It also may happen that a floating-point operation produces a value that is not representable as a number. Python represents this with the string 'NaN'.
Python String to Int, Int to String, Python String to int, Python int to String, python convert string to int, python a number that is represented in the string to int, you have to use int() function to do so. If the string you want to convert into int belongs to different number base other exception but you get wrong data """ num = '12' # this is a hexadecimal value Integers are whole numbers. In other words, they have no fractional component. Two data types you can use to store an integer in Python are int and str. These types offer flexibility for working with integers in different circumstances. In this tutorial, you’ll learn how you can convert a Python string to an int.
Python String to Int() Tutorial: Type Conversion in Python, The Python int method is used by coders to convert a string to an integer. two parameters: the initial string and the optional base to represent the data you are working with will be stored in a number of different ways. 12. The value the user inserted is 12 . This may look like a number. Add a chapter. In this program, you will learn to add two numbers and display it using print () function. To understand this example, you should have the knowledge of the following Python programming topics: In the program below, we've used the arithmetic addition operator + to add two numbers. Changing this operator, we can subtract (-), multiply (*), divide
Converting Python 3 Data Types: Numbers, Strings, Lists, Tuples , In Python, there are two number data types: integers and The int() function works similarly to the float() function: you can add a To convert the integer 12 to a string value, you can pass 12 into the str() method: str(12) So, our output looks unusual since it just places the two values next to each other. Converting Numbers to Strings. We can convert numbers to strings through using the str() method. We’ll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value. Let’s first look at converting integers. To convert the integer 12 to a string value, you can pass 12 into the str() method: str(12)
- First of all, your numbers are integer, not float. And you can convert a base-12 number string to an int with