0

I'm working on this question...and have encountered 2 problems that I am not smart enough to solve. Will you help?

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().

my code

user_input = int(input())

my_list = []

while True:
    if user_input >= 0:
        my_list.append(user_input)
        user_input = int(input())
    elif user_input <= 0:
        print(my_list[::-1])
        break

I need to stop when it hits the * but I don't know how to identify that in the code so for testing I set it to print when a negative number is entered.

I need to print the result in reverse without using reverse() but it has to be printed as a string and not a list. Can't figure out how to do that.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
Mac Jay
  • 31
  • 5
  • 3
    See the part of the assignment statement where it says "Use a while loop to output the integers."? This means: 1) Write a *separate* `while` loop, after you have finished creating the list. 2) Each time through that loop, output *one* of the values from the list. (Can you think of a rule that tells you which one to output?) – Karl Knechtel Feb 18 '22 at 21:25
  • 1
    You need to check if the input is `*` before converting it to an integer. – Barmar Feb 18 '22 at 21:25
  • Please also keep in mind that you are supposed to ask *one* question at a time (*after* you have made your best effort to understand and [research](https://meta.stackoverflow.com/questions/261592) the problem). – Karl Knechtel Feb 18 '22 at 21:27

3 Answers3

1

Check whether the input is * before converting it to an integer.

my_list = []

while True:
    user_input = input('Enter a number, or * to stop')
    if user_input = '*':
        break
    my_list.append(int(user_input))

while len(my_list) > 0:
    print(my_list.pop(), end=",") # remove the last element and print it
print()
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Thanks Barmer this is exactly what Random Davis explained above your code helped me visualize it, so thanks. I do have a question though why did you remove the last element in the print statement? – Mac Jay Feb 19 '22 at 00:27
  • @MacJay by removing the last element over and over again, and printing each removed element, you're printing the elements in reverse order. – Random Davis Feb 23 '22 at 16:04
0

So, let's break down the the assignment:

Write a program that reads a list of integers, one per line

This means that you should have a loop that reads in values, converts them into an integer and adds them to a list.

But:

until an * is read

This means that you first have to check that the user's input was equal to "*". But, you'll have to be careful to not try to convert the user's input into an integer before you check if it's an asterisk, otherwise an error will be thrown. So, your loop would look like the following:

  1. Get the user's input
  2. Check if it's an asterisk. If so, break the loop.
  3. If the user's input is not an asterisk, convert it to an int, and then add it to your list of inputted integers.

That's all you need for your first loop.

For the second loop, let's refer to the assignment:

then outputs those integers in reverse

This means that you will need a second loop; a for loop would work - you can have the loop go over a range(), except counting backwards. Here is a post about this exact thing.

  1. Your range would start at the length of your list, minus one, because the last index of the list is one less than the number of items, since list indices start at zero.
  2. Then, you end the range at -1, meaning the range will go down to zero, and stop, because it stops before it gets to the specified end.
  3. Your range will count down by -1, instead of the implicit +1 by default. Refer to that post I linked if you're not sure how to do this.

Lastly, we need to actually print the items at those indices.

follow each integer, including the last one, by a comma.

To me this means that either we could output one integer per line, with a comma at the end of each line, or we could print them all in one line, separated by commas.

If you want everything to print on one line, you can just do a regular print() of the item at the current index, followed by a comma, like print(my_list[a] + ',').

But, if you want to output them on separate lines, you can set the end argument of print() to be a comma. This post explains this.

And that's it!

There are other ways to go about this, but that should be a pretty straightforward and clear way to do it.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • 1
    Great explanation Random Davis I really appreciate you for taking the time to explain this to me. I also noticed below your write-up Barmar posted the code as you explained it so thanks to you both...you answered the question on how to check for the * and explained what I did wrong, much appreciated. – Mac Jay Feb 19 '22 at 00:25
-1

You can check if the user input equals a string with str(user_input) == '*'.

To print it off in reverse, you could print off the last value of the string repeatedly with print(my_list[:-1]) or use my_list.pop() to take and remove the last value.

I would change around your current code to:

my_list = []

while True:
        user_input = int(input())
        if str(user_input) == '*':
                for num in my_list:
                        print(my_list.pop())
                break
        else
                my_list.append(int(user_input))
Jonathan
  • 13
  • 6