python - 初学python3关于elif语句语法和print_function使用的问题
问题描述
尝试构建一个简单的脚本来返回磁盘、cpu 使用情况和网络连接检查。我的 elif 语句语法中不断出现错误。我已经尝试修改了 3 天,最后在 str 格式的第 36 行中没有出现错误,但现在我在第 37 行得到了一个无效的语法错误(仅包含“else:”的行。我在早期的 python 版本中读到过你有导入 print_function,这就是它被列出的原因,尽管我相信你不应该在 Python3 中这样做。任何关于为什么会发生此错误的建议或见解都将非常感激。
#!/usr/bin/env python3
import shutil
import psutil
import print_function
from network import *
def dsk_free(disk):
df = shutil.disk_usage(disk)
fre = df.free / df.total * 100
dsk_out = 'Disk usage = {}%'.format(fre)
return (dsk_out)
def cpu_ok():
cpuse = psutil.cpu_percent(1)
cpu_out = 'Cpu usage = {}%'.format(cpuse)
return (cpu_out)
def check_disk_usage(disk):
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
def check_cpu_usage():
usage = psutil.cpu_percent(1)
return usage < 75
x = dsk_free
y = cpu_ok
if not check_disk_usage("/") or not check_cpu_usage():
print("ERROR! Shut down immediately!")
elif check_connectivity() and check_localhost():
print("Everything is awesome! Disk Usage is {}% and Cpu usage is {}%".format(dsk_free(),cpu_ok())
else:
print("Network connection failure!")
解决方案
if not check_disk_usage("/") or not check_cpu_usage():
print("ERROR! Shut down immediately!")
elif check_connectivity() and check_localhost():
print("Everything is awesome! Disk Usage is {}% and Cpu usage is {}%".format(dsk_free(),cpu_ok())
else:
print("Network connection failure!")
在你的第二个 elif 语句中,你有你的打印语句,你错过了整理),在最后添加一个),你应该很高兴
推荐阅读
- javascript - Is it possible to use _.range with multiple steps?
- python - 根据条件更改熊猫中的列值
- python - Python:if not or 语句与嵌套 if 语句
- php - 解释 Eloquent morph许多参数
- java - 为什么这个使用 Java 查找素数的程序不起作用?
- r - 一种从重复测量中聚合数据的有效方法
- c# - 找到直角三角形的最后一个向量 - Unity
- perl - Perl 测试 - 测试的共同父级
- python-3.x - 如何返回生成器输出
- sql - 如何使用 SSIS 将多个变量循环到一个目标表中