I decided to learn python and I chose the book "The python book" to do so but I encountered a problem while coding one of the exercise programs, I'm doing a program that shows how control structures work but it gets stuck in a while loop, I think it is because a boolean variable (isint) is not setting to true so it just gets stuck there, but I'm not sure because I'm new to programming.
#!/usr/bin/env python2
import sys
target_int=raw_input("How many integers? ")
try:
target_int=int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints=list()
count=0
while count<target_int:
new_int=raw_input("Please enter integer{0}:".format(count+1))
isint=False
try:
new_int=int(new_int)
except:
print("You must enter an integer")
if isint==True:
ints.append(new_int)
count+=1
print("Using a for loop")
for value in ints:
print(str(value))
print("Using a while loop")
total=len(ints)
count=0
while count<total:
print(str(ints[count]))
count+=1
I would get this result everytime I ran the program:
jonathan@Jonathan:~/Python$ ./construct.py
How many integers? 1
Please enter integer1:2
Please enter integer1:3
Please enter integer1:4
Please enter integer1:4
Please enter integer1:23
Please enter integer1:13
As you can see no matter what I put there the while loop just keeps going.
isint, so it will always beFalse. Therefore, theifstatement will never get executed, meaningcountwill never be modified, meaning yourwhileloop will run forever.