爱学习520 发表于 2020-3-31 11:33:03

谁能解释一下这个呀

本帖最后由 爱学习520 于 2020-3-31 11:51 编辑

(x < y and or ) 实现什么样的功能?

谁能解释一下这个呀


这个是课后的习题,我没看懂这段,如图

永恒的蓝色梦想 发表于 2020-3-31 11:37:29

把代码全都发上来

一个账号 发表于 2020-3-31 11:39:23

请把完整代码发上来

永恒的蓝色梦想 发表于 2020-3-31 11:41:11

本帖最后由 永恒的蓝色梦想 于 2020-3-31 11:45 编辑

实现raise NameError("name 'x' is not defined")raise NameError("name 'y' is not defined")raise TypeError("'bool' object is not subscriptable")的功能{:10_256:}

一个账号 发表于 2020-3-31 11:43:31

永恒的蓝色梦想 发表于 2020-3-31 11:41
实现的功能

{:10_277:}

永恒的蓝色梦想 发表于 2020-3-31 11:43:51

一个账号 发表于 2020-3-31 11:43


{:10_256:}

qiuyouzhi 发表于 2020-3-31 11:45:20

永恒的蓝色梦想 发表于 2020-3-31 11:43


{:10_257:}

一个账号 发表于 2020-3-31 11:45:22

永恒的蓝色梦想 发表于 2020-3-31 11:43


>>> True
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    True
TypeError: 'bool' object is not subscriptable

{:10_256:}{:10_256:}{:10_256:}

永恒的蓝色梦想 发表于 2020-3-31 11:45:57

一个账号 发表于 2020-3-31 11:45


真的是巧了{:10_256:}Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> True
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    True
TypeError: 'bool' object is not subscriptable
>>> x
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    x
NameError: name 'x' is not defined
>>>

一个账号 发表于 2020-3-31 11:46:23

永恒的蓝色梦想 发表于 2020-3-31 11:41
实现的功能

你错了:

>>> x = 0
>>> y = 1
>>> (x < y and or )

>>> (x < y and or )
0

永恒的蓝色梦想 发表于 2020-3-31 11:47:39

一个账号 发表于 2020-3-31 11:46
你错了:

本来就是仅供娱乐的{:10_327:}{:10_264:}

郭书宇 发表于 2020-3-31 11:51:07

你可以看眼逻辑优先级
http://c.biancheng.net/view/2190.html
然后这是个例子
not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9

如果你的回答是 0,那么小甲鱼很开心你中招了!
答案是:4

not or and 的优先级是不同的:not > and > or

我们按照优先级给它们加上括号:(not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)
== 0 or 0 or 4 or 6 or 9
== 4

列表确保 x=0,y=0的情况不会判断失误
总的就是x<y 输出x反之y
https://fishc.com.cn/thread-67710-1-1.html
这是之前的人问的
然后列表比较你看一眼吧
>>> and

raimond 发表于 2020-3-31 12:03:10

print((2<3 and ['hello', 'abc'] or ))
str1 = 2<3 and ['hello', 'abc'] or
print(str1)

print((4<3 and ['hello', 'abc'] or ['python', 'world']))
str2 = 4<3 and ['hello', 'abc'] or ['python', 'world']
print(str2)
上面代码能看懂吗

爱学习520 发表于 2020-3-31 12:30:14

raimond 发表于 2020-3-31 12:03
print((2

>>> str1 = 2<3 and ['hello', 'abc'] or
>>> print(str1)
['hello', 'abc']
不知道为什么输出这个,这不是判断语句吗

永恒的蓝色梦想 发表于 2020-3-31 12:45:01

爱学习520 发表于 2020-3-31 12:30
>>> str1 = 2>> print(str1)
['hello', 'abc']
不知道为什么输出这个,这不是判断语句吗

对于 and 来说,如果前面的值是假值,就返回前面的值,否则返回后面的值
对于 or 来说,如果前面的值是真值,就返回前面的值,否则返回后面的值

_2_ 发表于 2020-3-31 12:49:53

永恒的蓝色梦想 发表于 2020-3-31 11:47
本来就是仅供娱乐的

看见切片就应该想到列表啊{:10_256:}

永恒的蓝色梦想 发表于 2020-3-31 12:50:38

_2_ 发表于 2020-3-31 12:49
看见切片就应该想到列表啊

字符串,元组表示不服{:10_256:}

_2_ 发表于 2020-3-31 12:51:40

永恒的蓝色梦想 发表于 2020-3-31 12:50
字符串,元组表示不服

……
不服就不服

raimond 发表于 2020-3-31 12:54:52

爱学习520 发表于 2020-3-31 12:30
>>> str1 = 2>> print(str1)
['hello', 'abc']
不知道为什么输出这个,这不是判断语句吗

a = 2<3
b =['hello', 'abc'] or
print(a)#True
print(b)#['hello', 'abc']
print(a and b )
print(True and ['hello', 'abc'])

拆分一下 看看

xiangjianshinan 发表于 2020-3-31 12:58:42

这么多人回复呀,学习了!!!这样浓厚的学习氛围真好!!!
页: [1] 2
查看完整版本: 谁能解释一下这个呀