Skip to main content
 首页 » 编程设计

python之使用简单程序进行字符串比较的意外打印输出

2024年10月24日29mfrbuaa

<分区>

我正在制作一个程序,要求用户输入他们想知道的单词在列表中的位置。但是我遇到了一个问题。

当我运行我的程序并提供单词“ask”作为输入时,它不会打印句子“Your number is in the num 's位置!”。我希望它被打印两次,因为这个词在列表中恰好在那些位置出现了两次。

相反,输出是:

1 
10 
None 

这是我的代码:

food = "ask not what your country can do for you ask what you can do for your country" 
food2 = food.split() 
print (food) 
 
word = (input("Enter Word: ")) 
 
for (num, x) in enumerate(list(food2)): 
    if word == x: 
        print (num + 1) 
        if num == "1" and "10" : 
            print ("Your number is in the ",num,"st position!")