Subtraction and Division are not working in this python example. Run the example and notice the output is correct for addition and multiplication. For subtraction the first long integer is displayed. For division the first long integer is displayed as a float.
import random
string1 = ''
string2 = ''
length1 = random.randrange(50,100)
length2 = random.randrange(50,100)
for i in range(length1):
string1 = str(random.randrange(0,10)) + string1
for i in range(length2):
string2 = str(random.randrange(0,10)) + string2
print('Random Length Random Number String')
print(string1, len(string1))
print(string2, len(string2))
if len(string2) > len(string1):
lenDiff = len(string2)-len(string1)
for i in range(lenDiff):
string1 = '0'+string1
if len(string1) > len(string2):
lenDiff = len(string1)-len(string2)
for i in range(lenDiff):
string2 = '0'+string2
#same length displays
print('Same Length Strings')
print(string1, len(string1))
print(string2, len(string2))
#convert to integers
listInt1 = [int(x) for x in string1]
listInt2 = [int(x) for x in string2]
print('Convert to Integers')
print('Integer 1')
print(listInt1)
print('Integer 2')
print(listInt2)
twoNum = listInt1 # temporary fill the answer numbers
# sum integers
for i in range(len(string1)):
twoNum[i] = listInt1[i] + listInt2[i]
print('Sum Integers')
print(twoNum)
# subtract integers NOT WORKING
for i in range(len(string1)):
twoNum[i] = listInt1[i] - listInt2[i]
print('Subtract Integers')
print(twoNum)
# multiply integers
for i in range(len(string1)):
twoNum[i] = listInt1[i] * listInt2[i]
print('Multiply Integers')
print(twoNum)
# divide integers NOT WORKING
for i in range(len(string1)):
if listInt2[i] > 0:
twoNum[i] = listInt1[i] / listInt2[i]
print('Divide Integers')
print(twoNum)
I looked up solutions and none exist to my knowledge
I tried various forms of subtraction but got the same result
New contributor
oldefellah52 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6