in python:
while int(input()):
# my code
use 4 spaces for indentatioon
Sorry. It's been too long since I've done C++, and your code didn't immediately compile. So I can't tell you why the C++ would accept a character. (I think it shouldn't since x is declared int).
But in the C++ code, cin is returning a value representing success, not the value of the input (that goes to x). So entering a "false" value still enters the loop.
In python, the input returns the value that was entered. This is passed to int() that tries to convert it. So if you enter a zero, it becomes the number zero, and that is false. There is no question of the input failing. It will block, succeed, or throw an exception.
In C++ a character can't be sent to the declared int variable, so the >> operator fails and returns false to the test.
In Python, int() doesn't fail. It either succeeds or throws an exception. You can catch the ValueError exception in a try/except block if you want to continue even when a non-integer is passed in.
The two act differently because they are doing completely different things. "cin >> x" returns fail() or !fail(). Since "x" is an "int" it returns !fail() as long as you keep entering integers. The value for x is ignored. If you enter something that is not an integer cin >> returns fail(). Your C++ loop will continue to execute as long as you keep entering integers.
The python code tests the value entered and uses that to determine when the loop ends. Zero is the only integer that evaluates to False, so entering zero is the only way to end the loop. All other numbers evaluate to True for the while loop test.
If you enter something other than an integer the python program throws an exception. This is normal for Python programs and should be thought of as the rough equivalent of returning fail() in the C++ program. In Python it is more common to try code and catch an exception. In C++ it is more common to provide a status return value that the program has to check.
in Python when i enter 0 the loop skipped but in C++ loop will continue
in Python when i enter a character I see a ValueError but in C++ the will skipped
this C++ code is a part of code in univercity and I wrote the Python code for comparison
sorry I can't write better in English
Python:
while int(input()):
print("-------")
print("out of loop")
Enter 1,2,3,0 :
Output:
1
-------
2
-------
3
-------
0
out of loop
Enter 1,2,3,E :
Output:
1
-------
2
-------
3
-------
E
Error:
ValueError: invalid literal for int() with base 10: 'E'
C++:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i=1;
while(cin>>i)
{
cout<<"------\n";
}
cout<<"press any key to continue ... ";
_getch();
}
Enter 1,2,3,0 :
Output:
1
------
2
------
3
------
0
------
needs more inputs ...
Enter 1,2,3,E :
Output:
1
------
2
------
3
------
E
press any key to continue ...
In line 1 (Python) you input a value. If that value tests to True (anything other than 0), the loop proceeds. If the 0 is entered (False) the loop terminates. If the entered value is not a number you trip a ValueError.
That's the Python behavior. Can't vouch for the C++. Learned C but skipped C++ as each version seemed OS specific so moved to Java before Python.