-2

I am trying to create a simple Python script using v3.8 to do the following:

  1. Create 2 seperate lists
  2. Have the lists grow based off user input (you exit by not entering an integer)
  3. Display the lists vertically side by side

Individually, I have all the items working. When I add my for loop, which comes after my try block, the script shuts down. If I put the for loop before my try block it works fine, which tells me it's coming from my try block. However, no matter how many times I try to google the answer and try different methods I just can't figure it out.

Code:

nameList = []
countList = []

try:

    while True:
        nameList.append(str(input("Enter Name: ")))
        countList.append(int(input("Enter Count: ")))

except Exception:
    pass
        
print("Name" '\t' "Count")
for o in range(len(nameList)):
    print(nameList[o] + '\t ' + countList[o])

sleep(2)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Krunch
  • 46
  • 8
  • Have you tried getting rid of the `except` ? `except Exception` is usually bad practice anyway, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Aug 20 '20 at 19:31
  • The 1st thing you should do if something does not work is to check for error messages, and read what it says. Running your code should have given you "*TypeError: can only concatenate str (not "int") to str*". That is not an "unknown error" and [there are a lot of related posts about it](https://www.google.com/search?q=site%3Astackoverflow.com+%22TypeError%3A+can+only+concatenate+str+(not+%22int%22)+to+str%22). – Gino Mempin Aug 20 '20 at 23:55
  • Does this answer your question? [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – Gino Mempin Aug 20 '20 at 23:59

3 Answers3

1

There are two problems:

  1. the line nameList[o] + '\t ' + countList[o] does not work because you are trying to append a string with a number. Use nameList[o] + '\t ' + str(countList[o]) instead
  2. When an exception in the while loop shows up the lists do not have the same length

The following will work, however it is definitely not best practise to write code like that

nameList = []
countList = []

try:

    while True:
        nameList.append(str(input("Enter Name: ")))
        countList.append(int(input("Enter Count: ")))

except Exception:
    pass
        

print("Name" '\t' "Count")
for o in range(len(countList)):
    print(nameList[o] + '\t ' + str(countList[o]))
Jonas
  • 91
  • 2
  • A way to handle the uneven length is to use [itertools.zip_longest](https://docs.python.org/3/library/itertools.html#itertools.zip_longest). – Gino Mempin Aug 21 '20 at 00:04
0

If you don't want to bother with the casting let format handle this for you by using placeholders in your string

print('{}\t {}'.format(nameList[o], countList[o]))
YouryDW
  • 393
  • 1
  • 7
-1

You can only concatenate str in your last print statement (countList[o] is an int type).

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135