python - 列出函数在 if 条件下不起作用
问题描述
嗨,我是编程新手,我喜欢尝试让代码以不同的方式工作。不幸的是,打印时无法读取名为 unlucky 的变量。因此,当用户输入列出的值 [7 6 5] 时,它不会打印为“您选择了不幸的数字”。我已经包含了我的代码的图像和副本:
option = int(input("Guess the correct number between 0 to 10 : \nYour choice :"))
unlucky_number= [7,6,5] # Unlucky numbers, listed
if option == unlucky_number: # should print somewhere close when user enters list number
print("You have selected a unlucky number")
elif option== 6: # only 6 is correct
print ("Correct Guess")
elif option in range (0,4):
print("Not close")
else:
print ("Not in the range, choose between 1 to 10")
请告诉我它有什么问题以及如何制作更好的版本。感谢您在此处输入图片描述
解决方案
if option == unlucky_number 这行给你带来了麻烦。您的“选项”包含一个单数,但您的“unlucky_number”是一个列表。列表不能等于一个数字,它们是完全不同的动物。你想要的是:如果 unlucky_number 中的选项
推荐阅读
- internationalization - i18next-fs-backend 保存所有语言的缺失文件
- spark-streaming - Spark 流式传输:具有背压/控制功能的 Google Cloud Pub/Sub 的 DStream 连接器
- docker - 我无法从 Jenkins Docker Containers 卷曲到我的已部署应用程序
- extjs - EXTJS 7 areaChart 动态标记或标记自定义
- arm - 如何在 ARM 中找到十六进制数字的符号扩展?
- go - 如何检查值是否动态实现接口?
- java - 十六进制数据不会在我的 BLE 扫描仪应用程序中打印
- php - 如何在 Api-Platform 中为虚拟属性引入过滤功能?
- spring - AWS:基于 Cognito 身份验证角色限制 API 对资源的访问
- autodesk-forge - 尝试对 Forge BIM360 API 进行 API 调用时出现错误?