小编Ths*_*aek的帖子

Python:如果第一个变量是True,检查第二个变量?

我有一个脚本通过if语句检查bool然后执行代码.我想要做的是这样做:

if variable a is True, check if b is True and execute code
if variable a is False, execute the same code mentioned before
Run Code Online (Sandbox Code Playgroud)

我目前拥有的简化版本是:

if a:
    if b:
        print('foo')
else:
    print('foo')
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来做这个,不需要我写print('foo')两次?

python if-statement python-3.x

5
推荐指数
1
解决办法
3329
查看次数

当用户向下滚动然后返回时更改颜色

我有几个div垂直排列一个在另一个上面从现在开始我将调用面板.每个面板都具有视口的宽度和高度.所有面板始终具有相同的背景颜色.最初,那种颜色是黑色的.

每个其他面板都是空的,并且充当实际具有内容的面板之间的间隙.订单是这样的:

  1. 内容
  2. 无内容
  3. 内容
  4. 无内容
  5. 内容

我想要做的是,当用户向下滚动足以让内容不在视图中的面板,颜色应该从黑色变为白色.然后,一旦他们滚动到足够第二个面板,内容不在视野范围内,它就应该改回来.

这是我无法弄清楚的部分.到目前为止,我有一个使用我的代码的工作演示:

$(document).scroll(function() {

  var viewportHeight = $("html").outerHeight();
  var currentY = $(document).scrollTop();
  if (currentY % viewportHeight != 0) {
    lighten();
  } else {
    darken();
  }
});

function darken() {
  $("body").css("background-color", "black");
  $(".panel.content").css("color", "white");
}

function lighten() {
  $("body").css("background-color", "white");
  $(".panel.content").css("color", "black");
}
Run Code Online (Sandbox Code Playgroud)
html,
body,
.panel {
  width: 100%;
  height: 100%;
}

body {
  background-color: black;
  overflow-y: visible;
  overflow-x: hidden;
  transition: background-color 500ms linear;
}

.panel {
  border: 2px solid …
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery

5
推荐指数
1
解决办法
268
查看次数

Python readlines没有返回任何内容?

我有以下代码:

with open('current.cfg', 'r') as current:
    if len(current.read()) == 0:
        print('FILE IS EMPTY')
    else:
        for line in current.readlines():
            print(line)
Run Code Online (Sandbox Code Playgroud)

该文件包含:

#Nothing to see here
#Just temporary data
PS__CURRENT_INST__instance.12
PS__PREV_INST__instance.16
PS__DEFAULT_INST__instance.10
Run Code Online (Sandbox Code Playgroud)

但是出于某种原因,current.readlines()每次只返回一个空列表.

代码中可能存在一个愚蠢的错误或拼写错误,但我找不到它.提前致谢.

python file python-3.x

1
推荐指数
2
解决办法
8520
查看次数

标签 统计

python ×2

python-3.x ×2

css ×1

file ×1

html ×1

if-statement ×1

javascript ×1

jquery ×1