0

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.

2
  • You never modify isint, so it will always be False. Therefore, the if statement will never get executed, meaning count will never be modified, meaning your while loop will run forever. Commented Nov 30, 2015 at 3:44
  • Yeah, I thought about that, but being a total novice at this language I thought it was ok to leave it like that because that's the way it was written in the book, besides it said that once the while statement ended it was going to become true Commented Dec 2, 2015 at 18:48

3 Answers 3

1

Indentation is important in python:

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

This is two separate blocks of code: the if isint==True: part is not inside the while count<target_int: block.

You need to change it to this:

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

Additionally, isint is never set to anything but False, anywhere. So the body of your if statement is never going to execute.

You probably want to set isint to True when you know the input is a valid integer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, helped me a lot, this newbie embarrassment feeling is getting me now
1

You aren't setting isint flag to true when checking for an integer.

while count<target_int:
    new_int=raw_input("Please enter integer{0}:".format(count+1))
    isint=False
    try:
        new_int=int(new_int)
        isint=True
    except:
        print("You must enter an integer")
    if isint==True:
        ints.append(new_int)
        count+=1

Comments

0

First your indentation looks wrong for:

if isint==True:
    ints.append(new_int)
    count+=1

Then you should add isint = True in (at the end of) the block

try:
    new_int=int(new_int) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.