简体   繁体   中英

Python - If Statement With input() Not Functioning Inside def() Function

Everything in the code runs properly except def a() and def b() have and if statements that examine and input function though when I run the code it will lead to 'good for you' being printed no matter the input. For instance if I type False or Whatever into the code which should lead to different results, both lead to the response 'good for you' as though the input is always 'True' or 'true' . I haven't been coding for very long so excuse me if this is an obvious fix.

    tsil = ['input',]

while True:
  print('Write A Number (Print "done" When Finished)')
  num = input()
  tsil.append(num)
  print()
  if num == 'done':
    break

if True == True:
  print(tsil)

def a():
  print('you like short lists? (True or False)')
  ans = input()
  if ans == 'True' or 'true':
    return '\ngood for you'
  elif ans == 'False' or 'false':
    return '\nstop making short lists then'
  else:
    return '\nstop printing the wrong things loser'


def b():
  print('you like long lists? (True or False)')
  ans = input()
  if ans == 'True' or 'true':
    return '\ngood for you'
  elif ans == 'False' or 'false':
    return '\nstop making short lists then'
  else:
    return '\nstop printing the wrong things loser'

if len(tsil) < 10:
  print('^ short list large gorge')
  print()
  print(a())
else:
  print('^ big boy list')
  print()
  print(b())

You need to change your if statement from if ans == 'True' or 'true': to if ans == 'True' or ans == 'true':

See below code:

def a():
  print('you like short lists? (True or False)')
  ans = input()
  if ans == 'True' or ans == 'true':   # if ans.lower() == 'true':
    return '\ngood for you'
  elif ans == 'False' or 'false':
    return '\nstop making short lists then'
  else:
    return '\nstop printing the wrong things loser'

Reasoning

If you check ans == 'True' or 'true will always generate 'True' which is 1st value in OR condition.

bool('any value') is always True

Take a look a this line carefully ans == 'True' or 'true'

This will always return True

You can try

print(bool('False' == 'True' or 'true'))
print(bool(-999 == 'True' or 'true'))
print(bool('Unicorn' == 'True' or 'true'))

to see its truth-value.

To solve the issue, you can replace ans == 'True' or 'true' with

if ans in ['True', 'true']:

or

if ans.lower() == 'true':

hope this helps.

if ans == 'True' or 'true'

should be

if ans == 'True' or  ans == 'true'

and same for other similar cases, since if 'non-empty string' evaluates to True

Your problem is that your conditions in the functions are as follows:

ans == 'True' or 'true'

The python interpreter sees it as being:

(ans == 'True') or ('true')

and a non empty string is evaluated to true when used in an if statement.

Change it to this:

ans == 'True' or ans == 'true'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM