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
1

4 Answers 4

3
while True:
    exchangeCurrency = input("what currency would you like to convert to: ").upper()
    if exchangeCurrency in myList:
        break
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Can save two lines with an assignment expression: while (userInput := input("Enter a currency: ").upper()) not in myList:
@flakes I do know this is a valid option but it is usually better to not give a one-linear to a beginner.
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")

Comments

-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

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.