C++ vs Python implementation [closed]
My C++ is functioning as expected but the equivalent Python code hangs in an infinite loop. Help!
C++
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<4;++i){
int j=0;
while(i!=j){
++j;
cout<<j<<endl;
}
}
}
Python
for i in range(4):
j = 0
while i != j:
++j
print(j)
python c++ loops
closed as unclear what you're asking by eyllanesc, phuclv, gnat, ead, Suraj Rao Nov 29 '18 at 7:28
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
My C++ is functioning as expected but the equivalent Python code hangs in an infinite loop. Help!
C++
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<4;++i){
int j=0;
while(i!=j){
++j;
cout<<j<<endl;
}
}
}
Python
for i in range(4):
j = 0
while i != j:
++j
print(j)
python c++ loops
closed as unclear what you're asking by eyllanesc, phuclv, gnat, ead, Suraj Rao Nov 29 '18 at 7:28
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
Please don't get into the habit of doing line-by-line translations from one language like C++ to another, and vice-versa. Learn the language as if the other language doesn't exist.
– PaulMcKenzie
Nov 24 '18 at 3:39
Paul has a point ... or many ...what is the purpose of the loop?
– Ted Lyngmo
Nov 24 '18 at 3:40
Well, I did try += as well but it didn't work in the original implementation for some reason. At least now I know why python doesn't use increment operator haha.
– Tejas Pandey
Nov 24 '18 at 3:44
possible duplicate: Why are there no ++ and -- operators in Python?, Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?, Behaviour of increment and decrement operators in Python
– phuclv
Nov 24 '18 at 4:05
Possible duplicate of Behaviour of increment and decrement operators in Python
– ead
Nov 29 '18 at 7:26
add a comment |
My C++ is functioning as expected but the equivalent Python code hangs in an infinite loop. Help!
C++
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<4;++i){
int j=0;
while(i!=j){
++j;
cout<<j<<endl;
}
}
}
Python
for i in range(4):
j = 0
while i != j:
++j
print(j)
python c++ loops
My C++ is functioning as expected but the equivalent Python code hangs in an infinite loop. Help!
C++
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<4;++i){
int j=0;
while(i!=j){
++j;
cout<<j<<endl;
}
}
}
Python
for i in range(4):
j = 0
while i != j:
++j
print(j)
python c++ loops
python c++ loops
edited Nov 24 '18 at 11:02
slmatrix
17611
17611
asked Nov 24 '18 at 3:36
Tejas PandeyTejas Pandey
175
175
closed as unclear what you're asking by eyllanesc, phuclv, gnat, ead, Suraj Rao Nov 29 '18 at 7:28
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by eyllanesc, phuclv, gnat, ead, Suraj Rao Nov 29 '18 at 7:28
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
Please don't get into the habit of doing line-by-line translations from one language like C++ to another, and vice-versa. Learn the language as if the other language doesn't exist.
– PaulMcKenzie
Nov 24 '18 at 3:39
Paul has a point ... or many ...what is the purpose of the loop?
– Ted Lyngmo
Nov 24 '18 at 3:40
Well, I did try += as well but it didn't work in the original implementation for some reason. At least now I know why python doesn't use increment operator haha.
– Tejas Pandey
Nov 24 '18 at 3:44
possible duplicate: Why are there no ++ and -- operators in Python?, Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?, Behaviour of increment and decrement operators in Python
– phuclv
Nov 24 '18 at 4:05
Possible duplicate of Behaviour of increment and decrement operators in Python
– ead
Nov 29 '18 at 7:26
add a comment |
3
Please don't get into the habit of doing line-by-line translations from one language like C++ to another, and vice-versa. Learn the language as if the other language doesn't exist.
– PaulMcKenzie
Nov 24 '18 at 3:39
Paul has a point ... or many ...what is the purpose of the loop?
– Ted Lyngmo
Nov 24 '18 at 3:40
Well, I did try += as well but it didn't work in the original implementation for some reason. At least now I know why python doesn't use increment operator haha.
– Tejas Pandey
Nov 24 '18 at 3:44
possible duplicate: Why are there no ++ and -- operators in Python?, Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?, Behaviour of increment and decrement operators in Python
– phuclv
Nov 24 '18 at 4:05
Possible duplicate of Behaviour of increment and decrement operators in Python
– ead
Nov 29 '18 at 7:26
3
3
Please don't get into the habit of doing line-by-line translations from one language like C++ to another, and vice-versa. Learn the language as if the other language doesn't exist.
– PaulMcKenzie
Nov 24 '18 at 3:39
Please don't get into the habit of doing line-by-line translations from one language like C++ to another, and vice-versa. Learn the language as if the other language doesn't exist.
– PaulMcKenzie
Nov 24 '18 at 3:39
Paul has a point ... or many ...what is the purpose of the loop?
– Ted Lyngmo
Nov 24 '18 at 3:40
Paul has a point ... or many ...what is the purpose of the loop?
– Ted Lyngmo
Nov 24 '18 at 3:40
Well, I did try += as well but it didn't work in the original implementation for some reason. At least now I know why python doesn't use increment operator haha.
– Tejas Pandey
Nov 24 '18 at 3:44
Well, I did try += as well but it didn't work in the original implementation for some reason. At least now I know why python doesn't use increment operator haha.
– Tejas Pandey
Nov 24 '18 at 3:44
possible duplicate: Why are there no ++ and -- operators in Python?, Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?, Behaviour of increment and decrement operators in Python
– phuclv
Nov 24 '18 at 4:05
possible duplicate: Why are there no ++ and -- operators in Python?, Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?, Behaviour of increment and decrement operators in Python
– phuclv
Nov 24 '18 at 4:05
Possible duplicate of Behaviour of increment and decrement operators in Python
– ead
Nov 29 '18 at 7:26
Possible duplicate of Behaviour of increment and decrement operators in Python
– ead
Nov 29 '18 at 7:26
add a comment |
2 Answers
2
active
oldest
votes
++j
is not a thing in Python. You want j += 1
.
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
@TejasPandey why don't you read the interpreter error? Googling for things likepython "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...
– phuclv
Nov 24 '18 at 4:02
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
add a comment |
In order to avoid ambiguity/confusion, our Benevolent Dictator For Life thought to not allow ++
or --
into the Python ecosystem. That means you are in an infinite loop because ++j
does not do what you believe it to do.
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.x = 5
x++
makes sensex = 'blah'
x++
doesn't make sense.
– Tejas Pandey
Nov 24 '18 at 17:44
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
++j
is not a thing in Python. You want j += 1
.
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
@TejasPandey why don't you read the interpreter error? Googling for things likepython "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...
– phuclv
Nov 24 '18 at 4:02
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
add a comment |
++j
is not a thing in Python. You want j += 1
.
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
@TejasPandey why don't you read the interpreter error? Googling for things likepython "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...
– phuclv
Nov 24 '18 at 4:02
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
add a comment |
++j
is not a thing in Python. You want j += 1
.
++j
is not a thing in Python. You want j += 1
.
answered Nov 24 '18 at 3:38
John ZwinckJohn Zwinck
153k17177294
153k17177294
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
@TejasPandey why don't you read the interpreter error? Googling for things likepython "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...
– phuclv
Nov 24 '18 at 4:02
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
add a comment |
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
@TejasPandey why don't you read the interpreter error? Googling for things likepython "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...
– phuclv
Nov 24 '18 at 4:02
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
I wasted too much time on this...
– Tejas Pandey
Nov 24 '18 at 3:40
@TejasPandey why don't you read the interpreter error? Googling for things like
python "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...– phuclv
Nov 24 '18 at 4:02
@TejasPandey why don't you read the interpreter error? Googling for things like
python "++" syntax error
would give you a lot of answers immediately: Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?...– phuclv
Nov 24 '18 at 4:02
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
I got an infinite loop so no error.
– Tejas Pandey
Nov 24 '18 at 4:52
add a comment |
In order to avoid ambiguity/confusion, our Benevolent Dictator For Life thought to not allow ++
or --
into the Python ecosystem. That means you are in an infinite loop because ++j
does not do what you believe it to do.
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.x = 5
x++
makes sensex = 'blah'
x++
doesn't make sense.
– Tejas Pandey
Nov 24 '18 at 17:44
add a comment |
In order to avoid ambiguity/confusion, our Benevolent Dictator For Life thought to not allow ++
or --
into the Python ecosystem. That means you are in an infinite loop because ++j
does not do what you believe it to do.
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.x = 5
x++
makes sensex = 'blah'
x++
doesn't make sense.
– Tejas Pandey
Nov 24 '18 at 17:44
add a comment |
In order to avoid ambiguity/confusion, our Benevolent Dictator For Life thought to not allow ++
or --
into the Python ecosystem. That means you are in an infinite loop because ++j
does not do what you believe it to do.
In order to avoid ambiguity/confusion, our Benevolent Dictator For Life thought to not allow ++
or --
into the Python ecosystem. That means you are in an infinite loop because ++j
does not do what you believe it to do.
answered Nov 24 '18 at 5:37
slmatrixslmatrix
17611
17611
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.x = 5
x++
makes sensex = 'blah'
x++
doesn't make sense.
– Tejas Pandey
Nov 24 '18 at 17:44
add a comment |
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.x = 5
x++
makes sensex = 'blah'
x++
doesn't make sense.
– Tejas Pandey
Nov 24 '18 at 17:44
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.
x = 5
x++
makes sense x = 'blah'
x++
doesn't make sense.– Tejas Pandey
Nov 24 '18 at 17:44
Well my understanding now is that all variables are immutable so you can't do ++. They are immutable because of how python handles datatypes.
x = 5
x++
makes sense x = 'blah'
x++
doesn't make sense.– Tejas Pandey
Nov 24 '18 at 17:44
add a comment |
3
Please don't get into the habit of doing line-by-line translations from one language like C++ to another, and vice-versa. Learn the language as if the other language doesn't exist.
– PaulMcKenzie
Nov 24 '18 at 3:39
Paul has a point ... or many ...what is the purpose of the loop?
– Ted Lyngmo
Nov 24 '18 at 3:40
Well, I did try += as well but it didn't work in the original implementation for some reason. At least now I know why python doesn't use increment operator haha.
– Tejas Pandey
Nov 24 '18 at 3:44
possible duplicate: Why are there no ++ and -- operators in Python?, Python integer incrementing with ++, in python, ++x is correct syntax. What does "++x" mean?, Does Python support ++?, Behaviour of increment and decrement operators in Python
– phuclv
Nov 24 '18 at 4:05
Possible duplicate of Behaviour of increment and decrement operators in Python
– ead
Nov 29 '18 at 7:26