Python list while loop check

Question:

my aim is to run the while loop until the user inputs a option that is in my list. If they do the while loop should end.

exchangeCurrency = input("what currency would you like to convert to: ").upper()

myList = ["USD", "ERU", "BRL", "JPY", "TRY"]

while exchangeCurrency != myList:
    print("this is not a valid inpit")
    continue
else:
    break
Asked By: John_Cooke

||

Answers:

a simple solution that may work

isTrue = True

while isTrue:
 if exchangeCurrency != myList:
   print("try again. Invalid currency")
 else:
   print("this currency is on the list")
   isTrue = False
Answered By: Squid

This would be the code:

myList = ["USD", "ERU", "BRL", "JPY", "TRY"]

userInput = input("Enter a currency: ").upper()
while userInput not in myList:
    print("Currency not found")
    userInput = input("Enter a currency: ").upper()
Answered By: Flow
while True:
    exchangeCurrency = input("what currency would you like to convert to: ").upper()
    if exchangeCurrency in myList:
        break
Answered By: logan_9997
myList = ["USD", "ERU", "BRL", "JPY", "TRY"]
while (exchangeCurrency := input("what currency would you like to convert to: ").upper()) not in myList:
    print("Error: currency not found")
Answered By: xFranko
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.