Home > Blockchain >  "if" statements not working - outputting certain code always user-input
"if" statements not working - outputting certain code always user-input

Time:04-11

No matter what it always outputs Spammer().

option = int(input("Enter the what you want to do: "))

<insert code not relevant>

if option == 1 or 'spam'.upper():
    Spammer()
elif option == 1:
    pcInfo()

Python 3.9.7 I've looked through several posts and nothing has worked.

CodePudding user response:

As 'spam'.upper() will return 'SPAN', which will always give True.

also, the pcInfo() will never execute, as it has the same condition as the one above.

CodePudding user response:

'spam'.upper()

will always return

SPAM

which is a non-empty string and therefore is regarded as a truthy statement by Python.

Since an if statement checks for truthy expressions, the if block will always be triggered.
If you need more help, please specify what you actually intend to with that statement.

Also: The else if block contains the same condition as the if block which I suppose is a typo? Otherwise if would always catch that case first and then skip over else if in all cases. So you should definitely use another value of the else if statement.

  • Related