0

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
flakes
  • 21,558
  • 8
  • 41
  • 88
  • Does this answer your question? [Check if something is (not) in a list in Python](https://stackoverflow.com/questions/10406130/check-if-something-is-not-in-a-list-in-python) – Ignatius Reilly Oct 18 '22 at 22:23

4 Answers4

3
while True:
    exchangeCurrency = input("what currency would you like to convert to: ").upper()
    if exchangeCurrency in myList:
        break
logan_9997
  • 574
  • 2
  • 14
1

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()
Flow
  • 846
  • 9
  • 24
  • Can save two lines with an assignment expression: `while (userInput := input("Enter a currency: ").upper()) not in myList:` – flakes Oct 18 '22 at 22:21
  • @flakes I do know this is a valid option but it is usually better to not give a one-linear to a beginner. – Flow Oct 18 '22 at 22:23
0
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")
Mohamed Darwesh
  • 659
  • 4
  • 17
-1

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
Squid
  • 27
  • 9