What's happening
It is not your program that freezes, it is the Python interpreter parser which gets stuck while reading the last line of your program. If you press the Enter key a few times, it gets unstuck and prints this
Traceback (most recent call last):
File "infiniteloop.py", line 1, in <module>
class InfiniteLoop:
File "infiniteloop.py", line 15, in InfiniteLoop
loop()
File "infiniteloop.py", line 13, in loop
input = input() #
File "<string>", line 0
I am no language lawyer so I cannot tell you what is actually happening there. In a class definition you do have the def function_name(self, param1, param2, ...): construct or that preceeded by an annotation. I do not have a slightest idea what happens if you try to call a function/method there.
What I do know is that judging by what you've probably tried to accomplish you have the syntax wrong.
Syntactically correct version
You have the indentation wrong on your last line. It should not be indented, it is not a part of that class, it should be on the same level as the class declaration.
A syntactically correct version of your program would be this
class InfiniteLoop:
def input():
print("Hello")
#num = raw_input("Enter a number: ")
#print "You entered: ", num
def loop():
global input
#input = input()
var = 1
while var == 1:
input = input() #
loop()
Of course, that would not run because there is not a top level function named loop anywhere. But syntax is now right.
The error looks like this
Traceback (most recent call last):
File "syntax", line 14, in <module>
loop()
NameError: name 'loop' is not defined
and as to how to fix it you can refer to one of the other answers. Most importantly of all, methods in Python do get their this object (actually self object, as it is customary to call it in Python) explicitly as their first parameter. So you have to declare the methods with def method_name(self, some, other, params):.
What would I do
Keep it simple. Do not declare a class when what you mean is just a couple of functions. And just to make sure, you no not have to declare a class in a Python program.
def get_user_input():
print("Hello")
num = raw_input("Write something and press Enter: ")
return num
def main():
while True:
input = get_user_input()
print "You entered: ", input
if __name__ == '__main__':
main()
The if __name__ construct is a useful way how to run something if the file is directly executed, but not to run the code if the file is included from some other place.
In conclusion
Read this SO question, answers, and the free to read Dive into Python book that is recommended there Learning Python coming from PHP ;)
classdoing in the first line?classis made valid, this isn't going to freeze, it's going to crash, as after the first call toloop,inputwill be set to the result of callinginput, which, assuming you've actually calledself.inputand thus not gotten aNameError, will now beNone, and therefor not be callable. Edit: Actually, that's not even true... you'll try to callinputfrom the global namespace, so unless it exists outside of class scope, you'll blow up on first run.loopif you ever instantiateInfiniteLoop, but it is technically syntactically valid, and will even run as written.