首页 > 解决方案 > 初学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!")

标签: python

解决方案


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 语句中,你有你的打印语句,你错过了整理),在最后添加一个),你应该很高兴


推荐阅读