1

I'm a rookie at programming. I want to create an open list that I can add to via inputs. The if statement I created does not work. It prints the list even if I enter Y in my response to the second input. I'm sure there are many issues with this but if someone could tell me what I'm doing wrong or if there is a better alternative it would be greatly appreciated.

tickers = []
print(f'current tickers list {tickers}')
f = input("please enter a ticker symbol you would like to watch:\n")
tickers.append(f)
q = input("would you like to add another ticker symbol Y or N: ")
if q == 'Y':
    print(f)
    tickers.append(f)
else: 
    print(f'Updated tickers list {tickers}')
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    You're adding the same ticker symbol. You need to ask for another ticker symbol. You can put the code that asks for all the inputs in a loop. – Barmar Nov 08 '21 at 19:00

2 Answers2

1
tickers = []

while True:
  print(f'current tickers list {tickers}')
  f = input("please enter a ticker symbol you would like to watch:\n")
  tickers.append(f)
  q = input("would you like to add another ticker symbol Y or N: ")
  if q == 'Y':
    pass
  else: 
    print(f'Updated tickers list {tickers}')
    break
Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
  • You don't need `break` there. Just initialize `q` as empty string before loop start, change condition to `while q != "N"` and move last print out of loop. – Olvin Roght Nov 08 '21 at 19:04
  • @OlvinRoght you're absolutely right, though depending on what OP is aiming for, a `while True` loop may communicate intent more directly? – Joshua Voskamp Nov 08 '21 at 19:05
  • @JoshuaVoskamp, I tend to avoid infinite loops unless it's the only possible method, same for `break`/`continue`. In this particular case it will make code slightly more efficient as two conditions will be replaced by one or better to say that one condition will be removed. – Olvin Roght Nov 08 '21 at 19:06
  • @OlvinRoght I hear you, though I suspect that may be a matter of preference. Myself, I see `if [condition]: break` at the end of a loop is easier to read, rather than seeing the condition in the `while [condition]` and then needing to scan the code inside the loop to see where the `condition` gets updated. – Joshua Voskamp Nov 08 '21 at 19:09
  • @JoshuaVoskamp, yes, you're absolutely right. Unfortunately, python doesn't have post-condition loop, so developers need to implement it by own. There're plenty of methods though, most of them shown in this question: [How to emulate a do-while loop?](https://stackoverflow.com/q/743164/10824407). – Olvin Roght Nov 08 '21 at 19:14
0

Think about what you want your if block to accomplish. When do you update the value of f? You might try e.g.

if q == 'Y':
    f = input("please enter another ticker symbol you would like to watch:\n")
    tickers.append(f)

Though as a commenter remarked, you may want to keep watching more symbols -- and you might not know how many. This is an excellent opportunity to use a while loop:

tickers = []
while True:
  print(f'Current tickers list {tickers}')
  f = input("Please enter a ticker symbol you would like to watch: ")
  tickers.append(f)
  q = input("Would you like to add another ticker symbol? (Y)es or (N)o: ")
  if q.casefold() not in 'no':
    break
Joshua Voskamp
  • 1,855
  • 1
  • 10
  • 13