- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试遵循 Python 缩进的正确协议(protocol),但 python 仍然会抛出错误。我确信 python 有充分的理由,我的代码很糟糕,但我没有看到根本原因。
运行时错误指向最后一行
~/python $ ./hover_api_v1.0.py
File "./hover_api_v1.0.py", line 139
time.sleep(60.0)
^
IndentationError: expected an indented block
下面是我的代码。有些标题与缩进无关。
错误是在时间命令的最后一行抛出的。但是我没有在代码中看到我的错误。时间 cmd 是顶部 while 循环的一部分并正确缩进。
while True:
ip_now = get_asus_wan_ip()
if (ip_now == ip_last):
day = datetime.datetime.now().day
if day != last_day:
last_day = day
with open(logfile, "a") as lf:
lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " WAN IP still the same " + str(ip_last) +"\n")
else:
# We need to do something
#print('WAN IP changed from ' + str(ip_last) + " to " + str(ip_now))
with open(logfile, "a") as lf:
lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " WAN IP changed from " + str(ip_last) + " to " + str(ip_now) + "\n")
ip_last= ip_now
# connect to API
client = HoverAPI('XXXXXXX','YYYYYYY')
for dnsname in ['*.zzzzz.zzz', '@.zzzzz.zzz']:
#print('Testing: ' + dnsname)
dns_name, domain_name = dnsname.split('.', 1)
# get all DNS records
result = client.call("get", "dns")
assert result['succeeded'], result
# discover existing dns record, if any
dns_record = None
domain_record = None
for dns_domain in result['domains']:
if dns_domain['domain_name'] == domain_name:
domain_record = dns_domain
for dns_entry in dns_domain['entries']:
if dns_entry['name'] == dns_name:
dns_record = dns_entry
break
if dns_record is not None and domain_record is not None:
break
if dns_record is not None and domain_record is not None:
#print('Hover-IP for ' + dnsname + ' = ' + str(dns_entry['content'].encode('ascii','ignore')))
#print('Current IP= ' + str(ip_now))
if str(dns_entry['content']) == str(ip_now):
#print('Hover-IP for ' + dnsname + ' = ' + str(dns_entry['content'].encode('ascii','ignore')) + ' same as Current IP = ' + str(ip_now) + '. No action.')
with open(logfile, "a") as lf:
lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Hover-IP for " + dnsname + " = " + str(dns_entry['content'].encode('ascii','ignore')) + " same as Current IP = " + str(ip_now) + ". No action." + "\n")
else:
#print(" Deleting entry for {0}.{1} ... ".format(dns_name, domain_name), end="")
with open(logfile, "a") as lf:
lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Deleting entry for " + dnsname + "\n")
result = client.call("delete", "dns/{0}".format(dns_record['id']))
assert result['succeeded'], result
#print("OK")
## create a new A record:
#print("Creating A record {0}.{1} => {2} ... ".format(dns_name, domain_name, ip_now), end="")
with open(logfile, "a") as lf:
lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Creating A record " + dnsname + " => " + ip_now + "\n")
record = {"name": dns_name, "type": "A", "content": ip_now}
post_id = "domains/{0}/dns".format(domain_record['id'])
#print("post", post_id, record)
result = client.call("post", post_id, record)
assert result['succeeded'], result
#print("OK")
else:
#print("No record exists for {0}".format(dnsname))
# Sleep at end of loop.
time.sleep(60.0)
非常感谢您的反馈。格特
最佳答案
问题就在这里;
else:
#print("No record exists for {0}".format(dnsname))
就缩进而言,注释不算作代码。所以你需要在那个地方有真正的代码。
解决这个问题的方法是使用 python 的 pass
关键字。
else:
#print("No record exists for {0}".format(dnsname))
pass
这向 python 发出信号,表明您有意将该缩进级别所需的代码留空。
或者,简单地取消注释你那里的代码也可以解决这个问题,当然假设你真的想在那里打印。
关于Python 缩进不直观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47343040/
我正在使用 libxml2 的 xmlwriter api 编写一个 xml 文件。 当我使用记事本打开文件时,缩进不正确。 有人知道怎么解决吗? 非常感谢。 最佳答案 我在这里有点冒进,但我会说“缩
我正在尝试让这个脚本工作,但它...给我缩进错误 #!/usr/bin/env python import io myfile = open('stats.txt', 'r') dan = myfil
我使用 Emacs 有一段时间了,我真的很想念一个古老的 Geany 快捷方式 - “C-i”和“C-u”。 “C-i”缩进整个当前行(将鼠标光标保持在原处),“C-u”取消整个当前行的缩进。 我发现
如何向 UILabel 内的文本添加缩进或偏移?它需要是特定的像素大小,与字体大小无关。 最佳答案 您可以创建另一个UILabel,然后将每个标签的框架设置为一定的宽度,这样,如果您想要实现这一目标,
请帮我在 Emacs haskell-mode 中设置正确的缩进 当我尝试输入诸如 ADT 或记录之类的内容时,按 后我进入了错误的列。 ,然后按 不会切换到右边,直到我输入 |或者 ';'! d
我在 Visio 2010 中有一个项目符号列表,我试图在其中缩进二级项目符号。例如: 我希望“子项目符号”项目向右缩进,这样很明显它是一个子元素。我认为功能区上的“增加缩进”选项可以做到这一点,但这
我写了这段代码: addNums key num = add [] key num where add res a:as b:bs | a == [] = res
我在生成的 xml 文档中添加了换行符。 "\n" some text etc."\n" "\n" 这最终应该是: some text etc. 这是否可以通过 google-code-pre
使用 JTabbedPane 时,如何缩进选项卡? Swing 默认输出: ------- --------- ------ | A | | B | | C | --------
我收到这些行的缩进错误 有没有在线验证器可以帮助我? showAliveTests : (pageIndex, statusFilter) -> data= pageI
在 Python 中,当你写了 100 行代码而忘记在某个地方添加一堆循环语句时,你会怎么做? 我的意思是,如果您在某处添加一个 while 语句,您现在必须缩进它下面的所有行。这不像您可以戴上牙套并
我喜欢这样做,如 indesign 或 quark...段落缩进...图片 如何在 html 和 css 中做到这一点的正确方法 我不希望文字环绕图像...我喜欢保护整个左边的部分给图片留边距就可以了
我试过添加 10px 的内边距但没有成功。你可以看到它的一个例子lower down on this page . #menu li { float: left;
这个问题在这里已经有了答案: I'm getting an IndentationError. How do I fix it? (6 个答案) 关闭去年。 while 1 == 1:
您好,我正在尝试使用来自 C# 应用程序的收据打印机打印帐单/收据。 预期的输出是这样的: ITEM NAME QTY PRICE Banana Large Y
有没有办法在 JTextPane 中缩进一段文本? import javax.swing.*; import java.awt.*; import javax.swing.text.StyledDoc
我知道 #define 等通常从不缩进。为什么? 我目前正在编写一些代码,其中混合了#define、#ifdef、#else s、#endifs 等。所有这些通常与普通 C 代码混合在一起。 #def
我认为缩进在 YAML 中很重要。 我在 irb 中测试了以下内容: > puts({1=>[1,2,3]}.to_yaml) --- 1: - 1 - 2 - 3 => nil 我期待这样的事情:
我在带有 openmp 语句的 C++ 代码中使用 Vim。 而在我的 ~/.vimrc set ai " auto indent 我的问题:当我使用 openmp 语句(以 # 开头)时,光标会跳
我想使用 Megaparsec 解析一种基本的缩进语言。最初我使用的是 Parsec,我设法通过缩进正常工作,但现在我遇到了一些麻烦。 我一直在关注一个教程here这是我必须解析一种忽略缩进的语言的代
我是一名优秀的程序员,十分优秀!