0

How do I set a user input variable that will print the value of a corresponding index number?

This is the code, please help because I have been texting and emailing my professor for 2 days and and haven't received a reply.

fileName = input("Enter the name of the file, e.g. 'numbers.txt': ")

#Coutning number of lines
lineCount=0
with open(fileName, 'r') as userData:
    for i in userData:
        lineCount=lineCount+1

#converting index count to match linecount
blankIndex = [None]
userList = [blankIndex] + [userData]

print(lineCount)

while True:
    inputTarget = int(input("Enter the number of the line you wish to see, or press enter twice to exit: "))
    if inputTarget == "":
        print("Enjoy your data.  Goodbye!")
        break
    elif inputTarget == 0:
        print("Great, you broke it...*slow clap*")
        break
    else:
        print(userList[inputTarget])
martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

0

The stuff in your while loop looks mostly good. The problem is, you're not populating the list in the first place!

fileName = input("Enter the name of the file, e.g. 'numbers.txt': ")

# we don't need to know the number of lines
# we can just open the file and put it into a list of lines with `.readlines()`
with open(fileName, 'r') as f:
    userList = [None] + f.readlines()
    # alternatively, the following commented-out code is a long-form way to accomplish essentially the same thing:
    # userList = []
    # for line in f:
    #    userList.append(line)

Using this approach, we can now treat userList as what it is - a list of strings, which corresponds exactly with the line in the file (to make it 1-indexed instead of 0-indexed, we added None as the first element, like you're trying to do in your snippet). Given an index, we can take out the string at that index.

while True:
    try:
        inputTarget = int(input("Enter the number of the line you wish to see, or press enter twice to exit: "))
    except ValueError: # this is the correct way to handle not-a-number inputs  print("Enjoy your data. Goodbye!")
        break
    if inputTarget == 0:
        print("Great, you broke it...*slow clap*")
        break
    else:
        print(userList[inputTarget])
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

While reading the file, you may store the lines, to be able to access them later. Here's code with some adjustement on the if/else conditions

userList = []
with open(fileName, 'r') as userData:
    for i in userData:
        userList.append(i.rstrip())

while True:
    inputTarget = input("Enter the number of the line you wish to see, or press enter twice to exit: ")
    if inputTarget == "":
        print("Enjoy your data.  Goodbye!")
        break
    indexTarget = int(inputTarget)
    if 0 > indexTarget or indexTarget >= len(userList):
        print("Great, you broke it...*slow clap*")
        break
    else:
        print(userList[indexTarget])

To type 1 for first line, add a -1

indexTarget = int(inputTarget) - 1
azro
  • 53,056
  • 7
  • 34
  • 70
  • Had to make a small edit so that the input number matched the index number (the old 0, 1 nonsense) but otherwise this worked great. Thank you so much. – Kyle Campbell Jun 16 '21 at 20:02
  • @KyleCampbell Have you tried a `-1` ? see my edit. You shouldn't expect alll the answer from someone else, try by yourself ;) – azro Jun 16 '21 at 20:08