python - 如何在“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")
解决方案
非常感谢你,我所做的是我已经将它与字符串"1080"
(如所说的 furas)进行了比较,然后我if screen_resolution[5:] == 1080:
改为if screen_resolution[-4:] == "1080":
推荐阅读
- reactjs - 条纹 - 我如何在反应中保存卡片元素?
- python - Flattening embedded keys with Pandas
- python - Python packaging of weakly dependent projects
- sql - 将具有嵌套行集列类型的 DMV 加载到 SQL Server 表中
- javascript - 仅当从特定函数调用时返回 2 组坐标梯度的 JavaScript 函数才返回 0
- eclipse - 在 Eclipse 中的类文件中搜索和替换
- javascript - Remove array when timestamp is equal to now
- c++ - I can't assign a value to a 2d array
- python - Is a Python list the same list as defined by my data structures and algorithms textbook?
- javascript - Freeze a variable added to an eventlistener function