File Input Problem

python

#1

Hi, I have this program where you enter lotto numbers and it tells you if you entered all numbers from 1 to 99 and the file input ends with 0.

Here is the main program: DEMO.py

# Create a list of 99 Boolean elements with value False
isCovered = 99 * [False]
endOfInput = False

while not endOfInput:
    # Read numbers as a string from the console
    s = input("Enter a line of numbers separated by spaces: ")
    items = s.split()  # Extract items from the string
    lst = [eval(x) for x in items]  # Convert items to numbers

    for number in lst:
        if number == 0:
            endOfInput = True
        else:
            # Mark its corresponding element covered
            isCovered[number - 1] = True

# Check whether all numbers (1 to 99) are covered
allCovered = True  # Assume all covered initially
for i in range(99):
    if not isCovered[i]:
        allCovered = False  # Find one number not covered
        break

# Display result
if allCovered:
    print("The tickets cover all numbers")
else:
    print("The tickets don't cover all numbers")

And here is the file input: LottoNumbers.txt

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 0

But when I execute it in the command line I get this error:

08%20AM

What am I doing wrong. Doesn’t each line in LottoNumbers.txt get inputted and it is turned into a string by the function input()? Please help.

Thanks.

By the way, I tried making each line in LottoNumbers.txt into strings, so

" 1 2 3 4 5 6 7 8 9 10"

And that worked but the textbook didn’t have to do that.


#3

what python version are you running on your macbook? using input() in python2 will cause the input to be validated as code.


#4

When I type python --version in the terminal it says python 2. But I installed and use python 3. I searched up on the internet and they said inorder to use python3 temporarily in the terminal you have to type
alias python=‘python3’

And this worked. I don’t know why.


#5

alias python=‘python3’ will make python3 default version, if you just temporary want to execute python3 code you can just do: python3 script.py

ensuring you have the right python version is rather important.

Also, you could use virtualenv:

http://sourabhbajaj.com/mac-setup/Python/virtualenv.html

to set up unique environments for each python project, preventing system wide package install, and with virtualenvwrapper you can better manage your virtual environments and set python3 as default for a virtual environment.

Its surprising this isn’t mentioned within the stackoverflow question you get as first google result

you could also include #!/usr/bin/env python3 at the start of your script to ensure its executed with python3