-1

I have a python3 script running a FlaskAPI, I require either parameterA or parameterB but not both.

parameterA = request.form.get('parameterA')
parameterB = request.form.get('parameterB')

if (parameterA is None) or (parameterA == '') :
    print('Parameter A is missing')

I can check for Parameter A but how do I modify this so that it checks for either parameterA or parameterB but errors on both?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

1 Answers1

1

You can define an initial value when using get on dict. Using this feature you can remove checking for None because if the value is not present in the request.form it will initiate it with the empty string ().

After that you can check for either of the variables like below:

parameterA = request.form.get('parameterA','')
parameterB = request.form.get('parameterB','')

#if both of them are '' then you don't have any of them and it raises error
#if none of them are '' then you have both of them and it raises error
if (parameterA == '' and parameterB == '') or (parameterA != '' and parameterB != '') :
    print('Parameter A and B are not suitable')

Another fun way:

if ['',''] == [parameterA, parameterB] or not ('' in [parameterA, parameterB]) :
    print('Parameter A and B are not suitable')

First one checks for both empty value and the second one checks for the both passed.

Alireza HI
  • 1,873
  • 1
  • 12
  • 20