首页 > 解决方案 > 如何在“screen_resolution”变量中选择特定字符并使用 if/else 命令分别检查它们?

问题描述

我正在做一个系统,用户可以在 3 个可用的屏幕分辨率之间选择他们的屏幕分辨率,并在控制台中写入他们选择的分辨率(“1920 x 1080”或其他),如果他们没有用最后四个数字输入分辨率等于 1080、1024 或 900,它会自动在控制台中输入“错误”,我唯一的问题是它总是说值是错误的,即使我只输入数字 1080、1024 或 900。
我做了一个某处出错还是无法做我想做的事?

有关信息,我在 Windows 10 64 位计算机上使用 JetBrains PyCharm Community Edition 2019.1.1 x64(我的文件分析未检测到任何内容)。

代码

   screen_resolution = input \
    ("What is you're desired resolution (Choose between the 3 basic resolution: 1920 x 1080 (Native); "
     " 1600 x 1024; 1600 x 900) :")

if screen_resolution[5:] == 1080:
    screen_resolution = "1920 x 1080"
    print("Correct")
    print("The changes will be applied at the next Carventure launch. you chose " + screen_resolution + ".")

elif screen_resolution[4:] == 1024:
    screen_resolution = "1600 x 1024"
    print("Correct")
    print("The changes will be applied at the next Carventure launch. you chose " + screen_resolution + ".")

elif screen_resolution[3:] == 900:
    screen_resolution = "1600 x 900"
    print("Correct")
    print("The changes will be applied at the next Carventure launch. you chose " + screen_resolution + ".")

else:
    print("Wrong")

标签: python

解决方案


非常感谢你,我所做的是我已经将它与字符串"1080"(如所说的 furas)进行了比较,然后我if screen_resolution[5:] == 1080:改为if screen_resolution[-4:] == "1080":


推荐阅读