0

I have a code which is was written in C++. I want to convert this to Python 3, but there is some problem that starts from for(;;) loop to res+=arr[i]. There is my C++ pattern:

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n;
  vector<int> arr;

  for(;;){
    cin>>n;
    if(n==0) break;
    else arr.push_back(n);
  }

  int res = 0;

  for(int i=0;i<arr.size();i++){
    res+=arr[i];
  }

  cout<<res<<" "<<arr.size();


  return 0;
}

And I want to convert to Python 3.

from itertools import count

arr = []

for i in count(0):
  n = int(input())
  if(n==0): break
  else: arr.append(n)

res = 0

for i in arr:
  res+=arr[i]

print(res+" "+len(arr))

So there are several mistakes that are hidden from me because I am coding in C++.

ibragim
  • 5
  • 3
  • 2
    `for i in arr:` in Python this actually gives the *values* of the elements in `arr`, not indices from `0` to `len(arr) - 1`. So you'd just need `res += i`. – Nathan Pierson Oct 14 '20 at 13:31

4 Answers4

2

The C++ code is basically appending integers from user input until a 0 is encountered. The equivalent python code would be

arr = []
n = None
while True:
    n = int(input())
    if n == 0:
        break
    arr.append(n)

Then once it breaks out of the loop, it computes the sum of the values, in this case that is trivial in python

res = sum(arr)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • I want to use for loop instead of while, is it possible? – ibragim Oct 14 '20 at 13:30
  • 2
    A `while True` loop is more canonical in python, but [it is possible to write an infinite for loop](https://stackoverflow.com/questions/34253996/infinite-for-loops-possible-in-python) instead – Cory Kramer Oct 14 '20 at 13:31
  • 1
    Asker's input loop is actually behaving exactly the way it's supposed to, the problem is with the summation afterward. – Nathan Pierson Oct 14 '20 at 13:31
  • 1
    @ibragim Python's `for x in y:` doesn't correspond to C++'s `for(init;test;inc)`, but `for(auto x : y)` – Caleth Oct 14 '20 at 13:56
0

The only thing I see that's wrong is using i from for i in arr:. When you say for i in arr, i is the value in each element, and not the index like you're using it. So instead you would use it as res += i. Then instead of using a for i in count(0) loop to loop essentially forever, you could use a while True:

Jackson
  • 78
  • 1
  • 5
0

for(;;) is basically an infinit loop so you can replace that by while(True) in your python code. Also there is no += in python so it would be res = res + arr[i]. Also the for loop is a bit different:

for i in arr:
    res = res + i
Michael
  • 147
  • 4
0

I think that will help you

import sys

def cin(arr):
    for line in sys.stdin:
        for var in line.split():
            var = int(var)
            if var == 0:
                return arr
            arr.append(var)
def main():
    arr = []
    arr = cin(arr)
    res = 0
    for i in arr:
        res+=i
    print(str(res)+ ' ' + str(len(arr)))
main()
Sekojuku
  • 43
  • 5