-1

I'm trying to create an error message for if the user doesn't input one of the options they are given, there are multiple options.

This is what I've tried so far:

if crypto_choice == 'BTC' or 'BCH' or 'ETH' or 'LTC' or 'XRP':
    print("")
else:
    print("Error: Choose BTC, BCH or ETH")
    time.sleep(3)
    quit()

For example: I set crypto_choice to 'trees' The program then continues to run normally, ignoring the else, and the variable stays as trees.

I'm new to python and programming so I'm sorry if I'm being stupid, thanks in advance to anyone that helps me.

Josh Eaton
  • 21
  • 3
  • related: https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value basically you want to use `in` if you're going to test for multiple conditions – EdChum Jul 25 '19 at 12:25
  • 1
    `crypto_choice == 'BTC' or 'BCH' or 'ETH' or 'LTC' or 'XRP'` == `(crypto_choice == 'BTC') or ('BCH') or ('ETH') or ('LTC') or ('XRP')` and non-empty string evaluates to True – h4z3 Jul 25 '19 at 12:25
  • 2
    Just use `crypto_choice in {'BTC', 'BCH', 'ETH', 'LTC', 'XRP'}` – norok2 Jul 25 '19 at 12:26
  • @MrFuppes *multiple `a == b` conditions with `or` where one side of the equality is kept constant across conditions... – norok2 Jul 25 '19 at 12:29

3 Answers3

1

Using lists is more pythonic and easier.

    if crypto_choice in ['BTC', 'BCH', 'ETH', 'LTC', 'XRP']:
        print("")
    else:
        print("Error: Choose BTC, BCH or ETH")
        time.sleep(3)
        quit()
nidabdella
  • 811
  • 8
  • 24
0

You need to check if crypto_choice == each option. A more efficient way would be to make an array and use in:

if crypto_choice in ["BTC", "BCH", "ETH", "LTC", "XRP"]:
  print("")
else:
  print("Error: Choose BTC, BCH or ETH")
  time.sleep(3)
  quit()
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

you should do

if crypto_choice == 'BTC' or crypto_choice == 'BCH' or crypto_choice == 'ETH' or 
crypto_choice == 'LTC' or crypto_choice == 'XRP':
    print("")

else:
    print("Error: Choose BTC, BCH or ETH")