Python if语句总是正确的

Python if语句总是正确的,python,python-3.x,Python,Python 3.x,我正在尝试学习python,目前我正在尝试构建一个票证机,在本例中模拟支付步骤。因为这是一个用于演示目的的模拟,我要求用户通过布尔输入输入True或false。由于某种原因,当我输入False时,它仍将被解释为True。下面是我的代码: def payment(): global tries print("Payment can be done by PIN. Please follow the instructions on the payment terminal\n")

我正在尝试学习python,目前我正在尝试构建一个票证机,在本例中模拟支付步骤。因为这是一个用于演示目的的模拟,我要求用户通过布尔输入输入True或false。由于某种原因,当我输入False时,它仍将被解释为True。下面是我的代码:

def payment():
    global tries
    print("Payment can be done by PIN. Please follow the instructions on the payment terminal\n")
    time.sleep(2)
    payment_succeeded = bool(input("for demonstration purpose: did the payment succeed?"))
    if payment_succeeded is True:
        print("Payment succeeded.\n")
        trip_time()
        time.sleep(1)
    elif tries < 3:
        tries +=1
        print("Payment Failed. please try again\n")
        time.sleep(0.5)
        payment()
    else:
        print("Payment has failed. Stopping purchase.\n")
def payment():
全球尝试
打印(“可通过PIN进行支付。请按照支付终端上的说明\n”)
时间。睡眠(2)
payment_successed=bool(输入(“用于演示目的:支付成功了吗?”)
如果付款成功为真:
打印(“付款成功。\n”)
行程时间()
时间。睡眠(1)
elif<3:
尝试次数+=1
打印(“付款失败。请重试\n”)
睡眠时间(0.5)
付款()
其他:
打印(“付款失败。正在停止购买。\n”)
默认情况下,全局“trys”变量的值为1

bool()
不会将
'False'
转换为
False
。它只查看字符串的长度,只有空字符串被认为是false

因此,输入除空字符串以外的任何内容都被认为是正确的:

您最好对某些字符串进行测试:

response = input("for demonstration purpose: did the payment succeed?")
payment_succeeded = response.lower() in ('y', 'yes', 'it did', 'true', 'you betcha')
if payment_succeeded:   # no need for "is True" here
现在,输入除一个预期字符串之外的任何内容都将导致
支付成功
设置为

您可能希望通过提供预期的输入来限制这一点,并重新询问是否未提供:

while True:
    response = input("for demonstration purpose: did the payment succeed, yes or no?").lower()
    if response in ('y', 'yes', 'n', 'no'):
        payment_succeeded = response in ('y', 'yes')
        break
    print('Sorry, I did not understand that input, please try again.')
bool()
不会将
'False'
转换为
False
。它只查看字符串的长度,只有空字符串被认为是false

因此,输入除空字符串以外的任何内容都被认为是正确的:

您最好对某些字符串进行测试:

response = input("for demonstration purpose: did the payment succeed?")
payment_succeeded = response.lower() in ('y', 'yes', 'it did', 'true', 'you betcha')
if payment_succeeded:   # no need for "is True" here
现在,输入除一个预期字符串之外的任何内容都将导致
支付成功
设置为

您可能希望通过提供预期的输入来限制这一点,并重新询问是否未提供:

while True:
    response = input("for demonstration purpose: did the payment succeed, yes or no?").lower()
    if response in ('y', 'yes', 'n', 'no'):
        payment_succeeded = response in ('y', 'yes')
        break
    print('Sorry, I did not understand that input, please try again.')
bool()
不会将
'False'
转换为
False
。它只查看字符串的长度,只有空字符串被认为是false

因此,输入除空字符串以外的任何内容都被认为是正确的:

您最好对某些字符串进行测试:

response = input("for demonstration purpose: did the payment succeed?")
payment_succeeded = response.lower() in ('y', 'yes', 'it did', 'true', 'you betcha')
if payment_succeeded:   # no need for "is True" here
现在,输入除一个预期字符串之外的任何内容都将导致
支付成功
设置为

您可能希望通过提供预期的输入来限制这一点,并重新询问是否未提供:

while True:
    response = input("for demonstration purpose: did the payment succeed, yes or no?").lower()
    if response in ('y', 'yes', 'n', 'no'):
        payment_succeeded = response in ('y', 'yes')
        break
    print('Sorry, I did not understand that input, please try again.')
bool()
不会将
'False'
转换为
False
。它只查看字符串的长度,只有空字符串被认为是false

因此,输入除空字符串以外的任何内容都被认为是正确的:

您最好对某些字符串进行测试:

response = input("for demonstration purpose: did the payment succeed?")
payment_succeeded = response.lower() in ('y', 'yes', 'it did', 'true', 'you betcha')
if payment_succeeded:   # no need for "is True" here
现在,输入除一个预期字符串之外的任何内容都将导致
支付成功
设置为

您可能希望通过提供预期的输入来限制这一点,并重新询问是否未提供:

while True:
    response = input("for demonstration purpose: did the payment succeed, yes or no?").lower()
    if response in ('y', 'yes', 'n', 'no'):
        payment_succeeded = response in ('y', 'yes')
        break
    print('Sorry, I did not understand that input, please try again.')

支付调用本身是递归的,我认为这里不需要递归
bool(“False”)
的计算结果为
True
bool(“”
计算结果为
False
。删除
bool
并将语句更改为
如果支付成功为“True”:
从不写入
为True
为False
。支付调用本身是递归的,我认为您不希望在这里递归。提示
bool(“False”)
的计算结果为
True
bool(“”
计算结果为
False
。删除
bool
并将语句更改为
如果支付成功为“True”:
从不写入
为True
为False
。支付调用本身是递归的,我认为您不希望在这里递归。提示
bool(“False”)
的计算结果为
True
bool(“”
计算结果为
False
。删除
bool
并将语句更改为
如果支付成功为“True”:
从不写入
为True
为False
。支付调用本身是递归的,我认为您不希望在这里递归。提示
bool(“False”)
的计算结果为
True
bool(“”
的计算结果为
False
。如果付款成功,则删除
bool
,并将语句更改为
从不写入
为True
为False
。因此,例如,如果我不提供输入,它将被视为False?或者我在这里读错了吗?@ih16:如果你在没有键入任何其他内容的情况下按enter键,你会得到一个空字符串,
bool()
将转换为
False
,是的。你在那里读对了。所以,如果我不提供输入,它将被视为错误?或者我在这里读错了吗?@ih16:如果你在没有键入任何其他内容的情况下按enter键,你会得到一个空字符串,
bool()
将转换为
False
,是的。你在那里读对了。所以,如果我不提供输入,它将被视为错误?或者我在这里读错了吗?@ih16:如果你在没有键入任何其他内容的情况下按enter键,你会得到一个空字符串,
bool()
将转换为
False
,是的。你在那里读对了。所以,如果我不提供输入,它将被视为错误?或者我在这里读错了吗?@ih16:如果你在没有键入任何其他内容的情况下按enter键,你会得到一个空字符串,
bool()
将转换为
False
,是的。你读对了。