python - 在最后一个块中同时执行 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")
不知道为什么当我输入一个值时“您满足最低操作系统要求”和“不支持此操作系统”都打印。
解决方案
你的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")
推荐阅读
- node.js - 有没有办法将 discord.js 集合下载为文件并在机器人出现故障时恢复它?
- c# - 为什么 C# 中的 Image.Save() 并不总是给出相同的序列化结果以及如何以确定的方式序列化图像?
- java - 如何递归地制定二元微分问题?
- c# - 检测类属性C#中的变量值变化
- mysql - 从另一个表更新但加入计算列
- xml - XSLT - 重命名具有特定属性的所有子元素
- arm - Bare Metal Programming on STM32WB55 or STM32F10
- ejs - How do I update which option is selected based on EJS
- reporting-services - 如何在 SSRS (SQL Server Reporting Services) 的 iif 表达式中使用多值参数?
- python-3.x - How to count the elements in a "cell" with pandas