首页 > 解决方案 > 在最后一个块中同时执行 if 和 else 语句

问题描述

RAM = int(input("RAM amount: "))
if RAM >= 8:
 print ("Your RAM is good")
else:
 print ("not enough RAM")

HDD = int(input("Enter HDD or SSD storage space: "))
if HDD >=55:
 print("You have enough space")
else:
 print("You do not have enough space")

OS = input("Input Windows version ex: Windows 10: ")
if OS=="Windows 10":
 print ("You meet the minimum OS requirement")
if OS=="Windows 8.1":
 print ("You meet the minimum OS requirement")
if OS=="Windows 7":
 print ("You meet the minimum OS requirement")
if OS=="Linux":
 print("This OS is not supported")
if OS=="Mac":
 print("This OS is not supported")
else:
 print("Your OS does not meet the minimum requirements")

不知道为什么当我输入一个值时“您满足最低操作系统要求”和“不支持此操作系统”都打印。

标签: pythonif-statement

解决方案


你的if积木不是互补的。使用 elif 使它们互补。否则 else 语句将与最后一个 if 语句相补充,这正是您获得两个输出而不是一个输出的原因。

OS = input("Input Windows version ex: Windows 10: ")
if OS == "Windows 10":
    print ("You meet the minimum OS requirement")
elif OS == "Windows 8.1":
    print ("You meet the minimum OS requirement")
elif OS == "Windows 7":
    print ("You meet the minimum OS requirement")
elif OS == "Linux":
    print("This OS is not supported")
elif OS == "Mac":
    print("This OS is not supported")
else:
    print("Your OS does not meet the minimum requirements")

推荐阅读