布尔运算符:使用布尔变量进行分支 (python)
Boolean operators: Branching using Boolean variables ( python)
我正在做一道家庭作业题,我很难把它return 正确地写成最终陈述。
说明:
Write an expression that prints 'You must be rich!' if the variables
young and famous are both True.
代码(我只能更新 if 语句):
young = True
famous = False
if (young == 'True') and (famous == 'True'):
print('You must be rich!')
else:
print('There is always the lottery...')
我最初的想法是上面代码中的组合,但我很绝望,我也尝试了下面的所有组合:
if (young != 'True') and (famous == 'True'):
if (young == 'True') and (famous != 'True'):
if (young == 'True') and (famous != 'False'):
if (young == 'True') or (famous == 'True'):
if (young == 'True') or (famous != 'True'):
if (young == 'True') or (famous == 'True'):
if (young == 'True') or (famous != 'False'):
结果:
测试年轻名人都是假的
你的输出:总有彩票...
以年轻为真,以假为名进行测试
你的输出:总有彩票...
以年轻为假,以著名为真进行测试
你的输出:总有彩票...
✖ 以年轻人和名人为真进行测试
预期输出:你一定很有钱!
你的输出:总有彩票...
显然你混淆了布尔变量和字符串
young=True #boolean variable
和
young='True' #string
这是更正后的代码
young = True
famous = False
if young and famous:
print('You must be rich!')
else:
print('There is always the lottery...')
我建议你在使用这个之前学习一下关于字符串和布尔变量的课程,祝你好运
我正在做一道家庭作业题,我很难把它return 正确地写成最终陈述。
说明:
Write an expression that prints 'You must be rich!' if the variables young and famous are both True.
代码(我只能更新 if 语句):
young = True
famous = False
if (young == 'True') and (famous == 'True'):
print('You must be rich!')
else:
print('There is always the lottery...')
我最初的想法是上面代码中的组合,但我很绝望,我也尝试了下面的所有组合:
if (young != 'True') and (famous == 'True'):
if (young == 'True') and (famous != 'True'):
if (young == 'True') and (famous != 'False'):
if (young == 'True') or (famous == 'True'):
if (young == 'True') or (famous != 'True'):
if (young == 'True') or (famous == 'True'):
if (young == 'True') or (famous != 'False'):
结果:
测试年轻名人都是假的
你的输出:总有彩票...
以年轻为真,以假为名进行测试
你的输出:总有彩票...
以年轻为假,以著名为真进行测试
你的输出:总有彩票...
✖ 以年轻人和名人为真进行测试
预期输出:你一定很有钱!
你的输出:总有彩票...
显然你混淆了布尔变量和字符串
young=True #boolean variable
和
young='True' #string
这是更正后的代码
young = True
famous = False
if young and famous:
print('You must be rich!')
else:
print('There is always the lottery...')
我建议你在使用这个之前学习一下关于字符串和布尔变量的课程,祝你好运