skysthelimit91
skysthelimit91

Reputation: 179

Why am I getting a value error from my while loop?

I don't understand why I'm getting a Value Error. I specifically exclude the "*" character.....yet it still gets included and causes an error. I understand I have to figure out how to reverse the output too but I'll worry about that later.

Write a program that reads a list of integers, one per line, until an "*" is read, then outputs those integers in reverse. For simplicity in coding output, follow each integer, including the last one, by a comma.

Note: Use a while loop to output the integers. DO NOT use reverse() or reversed().

Ex: If the input is:

2
4
6
8
10
*

the output is:

10,8,6,4,2,

my code:

''' Type your code here. '''
while True:
    try:
        if input().strip() != "*":
            num = input().strip()
            lst = []
            lst.append(num)
            print("{},".format(int(num),end=""))
        else:
            break
    except EOFError:
        break 

Enter program input (optional)

2
4
6
8
10
*

Program errors displayed here

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    print("{},".format(int(num),end=""))
ValueError: invalid literal for int() with base 10: '*'

Program output displayed here

4,
8,

Upvotes: 2

Views: 607

Answers (2)

Timur Shtatland
Timur Shtatland

Reputation: 12405

Use append to add numbers to the list lst, and pop to remove them from the end of the list. Use f-strings or formatted string literals to print the list elements.

num = input().strip()
lst = []

while num != '*':
    num = int(num)
    lst.append(num)
    num = input().strip()

while lst:
    num = lst.pop()
    print(f"{num},", end='')
print('')

Comments on your code:


while True:
    try: # No need for the try block.
        if input().strip() != "*": # This input gets lost: not assigned to any variable.
            num = input().strip() # This input is not tested whether it is '*'.
            lst = [] # This list gets assigned anew ands becomes empty with every iteration.
            lst.append(num)
            print("{},".format(int(num),end="")) # Use f-strings instead (easier).
        else:
            break
    except EOFError:
        break

Upvotes: 1

cwichel
cwichel

Reputation: 11

It's because you're using input 2 times:

  • First input read 10 and it passes the if.
  • Second input read "*" inside the "action" section of your code and fails.

Also:

  • You're resetting the list on every loop. The initialization should be outside of the loop.
  • Using a more pythonic way to check if the "*" is in the data should avoid any further issues, for example, using "**" or having an incorrect input "1*".

An updated version of the code will be:

''' Type your code here. '''
lst = []
while True:
    try:
        data = input().strip()
        if "*" not in data:
            lst.append(num)
            print("{},".format(int(num),end=""))
        else:
            break
    except EOFError:
        break 

Upvotes: 1

Related Questions