首页 > 解决方案 > 列出函数在 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")

请告诉我它有什么问题以及如何制作更好的版本。感谢您在此处输入图片描述

标签: python

解决方案


if option == unlucky_number 这行给你带来了麻烦。您的“选项”包含一个单数,但您的“unlucky_number”是一个列表。列表不能等于一个数字,它们是完全不同的动物。你想要的是:如果 unlucky_number 中的选项


推荐阅读