-1

I am unable to enter more than one integer. I should be able to specify how many and then input like so...

How many integers? 3
Please enter an integer 1: 5
Please enter an integer 2: 2
Please enter an integer 3: 6
Using a for loop
5
2
6

However I am only being asked for integer 1 (in a continuous loop)

Below is the code:-

#!/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))
~                   
  • 1
    Set `isint = True` after typecast attempt in try clause – Jeremy Apr 23 '22 at 19:06
  • [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Apr 23 '22 at 19:06
  • Delete all lines *using* `isint`; then move `ints.append(new_int); count += 1` to the try suite. – wwii Apr 23 '22 at 19:09
  • 1
    don't use bare except. change it with `except ValueError` – Yoshikage Kira Apr 23 '22 at 19:09
  • Thank you, all. I use Pycharm at work (currently learning Python) and have used step through to debug. This time I just fired up my laptop and started editting in VIM. – manOnTheMoon Apr 23 '22 at 19:10

1 Answers1

1

Your main issue is as noted, that you aren't setting isint. But you shouldn't anyway, as you already cast in the try block. So if you reach this code, it is necessarily an int. 2 other small things are, I'm running in Python 3, since it was released 14 years ago, so raw_input is input, and it is good to get into the habit of initializing lists with [] instead of list(). It is faster sometimes, and never slower.

Here is working code:

import sys

target_int = input("How many integers?")

try:
    target_int = int(target_int)
except ValueError:
    sys.exit("You must enter an integer")

ints = []

count = 0

while count < target_int:
    new_int = input("Please enter integer {0}:".format(count + 1))
    try:
        new_int = int(new_int)
    except ValueError:
        print("You must enter an integer")
        continue # so it doesn't try to append

    # No need to check isint. It is.
    ints.append(new_int)
    count += 1

print("Using a for loop")
for value in ints:
    print(str(value))

Another small improvement is to avoid count altogether, since it is always the length of ints already.

import sys

target_int = input("How many integers?")

try:
    target_int = int(target_int)
except ValueError:
    sys.exit("You must enter an integer")

ints = []

while len(ints) < target_int:
    new_int = input("Please enter integer {0}:".format(len(ints) + 1))
    try:
        ints.append(int(new_int))
    except ValueError:
        print("You must enter an integer")


print("Using a for loop")
for value in ints:
    print(str(value))
theherk
  • 6,954
  • 3
  • 27
  • 52