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}')
1
  • 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. Commented Nov 8, 2021 at 19:00

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

5 Comments

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.
@OlvinRoght you're absolutely right, though depending on what OP is aiming for, a while True loop may communicate intent more directly?
@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.
@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.
@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?.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.