0

I tried to practice input/output redirection in the Python shell. I have a file named SentinelValue.py which is where I have this code to add up the numbers in another file:

data = eval(input("Enter an integer (the input ends " + "if it is 0): "))

sum = 0
while data != 0:
    sum += data

    data = eval(input("Enter an integer (the input ends " + "if it is 0): "))

print("The sum is", sum)

The other file "Numbers.txt" contains numbers:

1 
2 
3 
4 
5 
6 
7 
8 
9 
0

and my output.txt file is where I want the sum to show.

I tried using:

python SentinelValue.py < Numbers.txt > output.txt

but on shell, it highlights "SentinelValue" & says "invalid syntax". I don't know why it's not working.

DYZ
  • 55,249
  • 10
  • 64
  • 93
psquare
  • 25
  • 3
  • 3
    Did you run that command on the _OS shell_ command line (right) or _python_ command line (wrong)? – DYZ Aug 05 '18 at 04:44
  • 2
    And **do not ever use** `eval(input())` [(why?)](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – DYZ Aug 05 '18 at 04:46
  • You need to use lower level IO, `sys.stdin.read`, and `sys.stdout.write`. – Keith Aug 05 '18 at 05:38
  • I wrote it on the python command line. The book which had this exercise didn't say where to type in the redirection so I had assumed it would be the python command line. The book only covered loops, while and if statements, counting, and some other basic topics so far, and it hadn't mentioned anything about the OS command line. I'll look more into it. Thank you! – psquare Aug 05 '18 at 15:39

1 Answers1

0

There are several things wrong with your code:

  • As already suggested in the comments, do not use eval() for a direct user input (or pretty much any time, in 99% cases when you think you need it - you don't!). A simple int() conversion should be more than enough without all the dangers of eval().
  • Connected with the previous, eval() evaluates the input. Further more, on Python 2.x input() itself does the evaluation (it's an equivalent of eval(raw_input())) so whenever it encounters an invalid input it will pop a SyntaxError.
  • Even if it doesn't pop a SyntaxError, it will most certainly pop a TypeError as eval() expects a string and it will receive an integer from the inner input().
  • You're printing the "Enter an integer..." prompt to STDOUT which will result it ending up in your output.txt (where you redirect the STDOUT).
  • You're shadowing the built-in sum() by using it as one of your variables. That's a bad practice that can lead to many problems and unexpected results down the line.

So, with all that in mind, here's how to rewrite it to address these issues:

# Let's first make sure it works on Python 2.x and 3.x by shadowing the input
try:
    input = raw_input  # on Python 2.x use raw_input instead of input
except NameError:
    pass

result = 0  # tip: use a name that's not already built-in, like result
while True:  # loop 'forever'...
    data = input()  # don't print anything when asking for input
    try:
        data = int(data)  # don't print anything to STDOUT
        if not data:  # only if the integer is 0
            break
        result += data  # add to the final result
    except ValueError:  # risen if the input is not an integer...
        pass  # so just ignore it

print("The sum is: " + str(result))  # finally, print the result
zwer
  • 24,943
  • 3
  • 48
  • 66
  • As the OP said, "I tried to practice input/output redirection _in the Python shell_." That's the cause of the error. – DYZ Aug 05 '18 at 05:39