1

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())
1
  • 2
    if ans.lower() == 'true' Commented Jan 30, 2019 at 4:27

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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

Comments

1

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'

Comments

1

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

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.