简体   繁体   English

Python-If语句与input()在def()函数内部不起作用

[英]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. 除了def a()def b()都有代码外,代码中的所有内容都可以正常运行,并且如果我在运行代码时检查并input函数的语句,无论输入内容如何,​​都会打印出'good for you' 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' . 例如,如果我在代码中键入FalseWhatever ,这将导致不同的结果,则两者均会导致响应'good for you' ,就好像输入始终是'True''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': 您需要将if语句从if ans == 'True' or 'true':更改为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. 如果您检查ans == 'True' or 'true将始终生成'True' ,这是OR条件下的第一个值。

bool('any value') is always True bool('any value')始终为True

Take a look a this line carefully ans == 'True' or 'true' 仔细看这行ans == 'True' or 'true'

This will always return True 这将始终返回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 要解决此问题,您可以将ans == 'True' or 'true'替换为

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 与其他类似情况相同,因为if 'non-empty string'值为True

Your problem is that your conditions in the functions are as follows: 您的问题是函数中的条件如下:

ans == 'True' or 'true'

The python interpreter sees it as being: python解释器将其视为:

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

and a non empty string is evaluated to true when used in an if statement. 在if语句中使用时,非空字符串的计算结果为true。

Change it to this: 更改为此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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