首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python的打印不输出最后一个print()单词

Python的打印不输出最后一个print()单词
EN

Stack Overflow用户
提问于 2022-01-27 13:19:25
回答 4查看 81关注 0票数 1

我的代码运行得很完美,并且得到了我想要的结果。然而,最后一次打印(“焦虑:")并不是输出焦虑,而是只打印出没有标题的答案。

我已经在空闲3.10.1以及我的Python类自己的Python程序中多次运行这个程序,并且得到了完全相同的结果。绝对没有错误,但在最后一次打印语句之后也没有标题。

有人知道发生了什么事吗?我以前的代码中没有遇到过这样的问题,我是为我的在线课程而运行的。

谢谢!

代码语言:javascript
复制
busy = True
hungry = False
tired = True
stressed = False

happy = busy and not stressed
sad = hungry or tired 

print("Happy: " + str(happy)) 
print("Sad: " + str(sad))
print("Confused: "+ str(happy and sad))
print("Bored: " + str(not(happy or sad or busy))) 
print("Anxious: " + str(not happy) and (not sad) and (not stressed))
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-01-27 13:25:19

这只是一个逻辑错误:

代码语言:javascript
复制
busy = True
hungry = False
tired = True
stressed = False

happy = busy and not stressed
sad = hungry or tired 

print("Happy: " + str(happy)) 
print("Sad: " + str(sad))
print("Confused: "+ str(happy and sad))
print("Bored: " + str(not(happy or sad or busy))) 
print("Anxious: " + str(not happy and (not sad) and (not stressed)))

这是:

代码语言:javascript
复制
print("Anxious: " + str(not happy) and (not sad) and (not stressed)))

应:

代码语言:javascript
复制
print("Anxious: " + str(not happy and (not sad) and (not stressed)))
#or
print("Anxious: " + str((not happy) and (not sad) and (not stressed)))
票数 2
EN

Stack Overflow用户

发布于 2022-01-27 13:25:23

线

print("Anxious: " + str(not happy) and (not sad) and (not stressed))

评估为

print("Anxious: False" and False and True)

字符串总是计算为True,所以您要打印True and False and True的结果,它只是打印False

票数 0
EN

Stack Overflow用户

发布于 2022-01-27 13:26:27

它不起作用,因为对str的偏执不包括整个操作--它只是覆盖操作的not happy部分,因此,您所要做的就是给它添加一个新的偏执。

代码语言:javascript
复制
print("Anxious: " + str((not happy) and (not sad) and (not stressed)))
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70879246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档