0

I am trying to make a program that can add by literally counting. But for that I have 2 for loops which need to work together. for example if I input 3 and 2 the outside for loop iterates till "3" in the array and then another for loop iterates on the array till "2" in such a way that the outside for loop should(but doesn't) iterate with it and the position it is at eventually is printed out(which should be 5). How can I achieve this? because right now the inside loop will finish its iteration and break.

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
#print(arr[0])
a = str(input()) #first number
b = str(input()) #second number

for i in arr:
    if i == a:
        for j in arr:
            if j == b:
                print(i)
                break

this program outputs 3 for input 3 and 2 but I want 5

Manik
  • 573
  • 1
  • 9
  • 28
  • Can you post A sample output ?? – abheet22 Sep 23 '19 at 15:55
  • @abheet22 the output I want or the output this code gives? I want it to output 5 for 3 and 2 but this outputs 3 – Manik Sep 23 '19 at 15:57
  • Output you want for the input mentioned. – abheet22 Sep 23 '19 at 15:59
  • The easiest way would be to have a variable which is incremented with each iteration of first and second loop. – SergeyA Sep 23 '19 at 15:59
  • `input` already returns a `str` value; there's no need to call `str` on it. – chepner Sep 23 '19 at 16:03
  • @SergeyA I want to make a program that can literally add but if I simply use the inbuilt add it would defeat the purpose because then i could simply add `print(3 + 2)`. I want it to only iterate the array to give the answer. – Manik Sep 23 '19 at 16:08
  • I think this cannot be done with for loops without using + operator does anybody know/suggest any other way like multiprocessing? – Manik Sep 23 '19 at 17:35

2 Answers2

2

You could use another variable to keep track of the count, like this:

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

a = str(input())  # first number
b = str(input())  # second number

counter = 0
for i in arr:
    if i == a:
        for j in arr:
            if j == b:
                print(counter)
                break
            counter += 1
    counter += 1

We can write a program to achieve a similar behaviour significantly more simply:

a = int(input())  # first number
b = int(input())  # second number

counter = 0

for _ in range(a):
    counter += 1
for _ in range(b):
    counter += 1
print(counter)

and this has the advantage that we aren't restricted to inputs in arr.

Rob Bricheno
  • 4,467
  • 15
  • 29
  • I want to make a program that can literally add but if I simply use the inbuilt add it would defeat the purpose because then i could simply add `print(3 + 2)`. I want it to only iterate the array to give the answer. – Manik Sep 23 '19 at 16:05
  • Please can you figure out a way of doing this without using the + operator because using it to implement addition is autological and I also want to add carry ability to it which + does by itself. – Manik Sep 23 '19 at 17:34
0

Here is my solution:

arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
num_list = list(range(int(arr[-1])*2+1))

new_dict = dict()
for index, value in enumerate(num_list):
    new_dict[value] = set()
    for item1 in arr:
        for item2 in arr:
            if int(value) == int(item1) + int(item2):
                new_dict[value].add((item1, item2))

a = str(input())  # first number
b = str(input())  # second number

target = (a, b)

for key, value in new_dict.items():
    if target in value:
        print(int(key))

Hope it helps. I am interested if you can achieve the goal without using any "+". Please share if you find it. Thanks in advance!

Tyrion
  • 405
  • 8
  • 22