Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的代码运行得很好,但当我尝试搜索已经包含的字符时,它不是';不匹配,一直说找不到_Python_Arrays_If Statement - Fatal编程技术网

Python 我的代码运行得很好,但当我尝试搜索已经包含的字符时,它不是';不匹配,一直说找不到

Python 我的代码运行得很好,但当我尝试搜索已经包含的字符时,它不是';不匹配,一直说找不到,python,arrays,if-statement,Python,Arrays,If Statement,这是我的输出: 输入要输入的字母数3 输入字母表A 输入字母B 输入字母C 您输入的字母表是数组('u','ABC') A B C 输入要搜索的字母是否可用b 找不到 此外,我还附加了我的输出图像删除中断,它将找到它。下面是一个稍微好一点的代码。我对你的做了些改变 from array import * alphabets = array('u', []) lettersRange = int(input("Enter how many letters do you want to ent

这是我的输出:
输入要输入的字母数3
输入字母表A
输入字母B
输入字母C
您输入的字母表是数组('u','ABC')
A
B
C
输入要搜索的字母是否可用b
找不到


此外,我还附加了我的输出图像

删除
中断
,它将找到它。下面是一个稍微好一点的代码。我对你的做了些改变

from array import *

alphabets = array('u', [])

lettersRange = int(input("Enter how many letters do you want to enter "))
for i in range(lettersRange):
    addLetter = input("Enter Alphabet ")
    alphabets.append(addLetter)

print("alphabets that you've entered are ", alphabets)

for i in range(len(alphabets)):
    print(alphabets[i])

searchLetter = input("enter the letter to search if its is available or not")
count = 0
for i in alphabets:
    if i == searchLetter:
        print('The Letter "',searchLetter,'" is available and it\'s position is',count )

    else:
        print("Letter Not found")
        break

    count += 1

检查最后一段代码:

from array import *

alphabets = array('u', [])

lettersRange = int(input("Enter how many letters do you want to enter "))
for i in range(lettersRange):
    addLetter = input("Enter Alphabet ")
    alphabets.append(addLetter)

print("alphabets that you've entered are ", alphabets)

for i in range(len(alphabets)):
    print(alphabets[i])

searchLetter = input("enter the letter to search if its is available or not")
count = 0
for i in alphabets:
    if i == searchLetter:
        print('The Letter "',searchLetter,'" is available and it\'s position is',count )
    break
    count += 1

if(count==len(alphabets)):
    print("Not Found")
分解:该代码表示,对于字母表中的每个字母,如果该字母是搜索字母,则打印(“xyz”)。如果该字母不在字母表中,请打印(“未找到字母”)。然后打破循环。这是一个好主意,但不幸的是,循环只测试第一个字母a。然后,如果它不是searchLetter,它将打破循环。我建议:

searchLetter = input("enter the letter to search if its is available or not")
count = 0
for i in alphabets:
    if i == searchLetter:
        print('The Letter "',searchLetter,'" is available and it\'s position is',count )

    else:
        print("Letter Not found")
        break

    count += 1

如果(count==len(alphabets)):print(“Not Found”)////你能解释一下我编辑代码的原因吗。本质上,如果找不到元素,那么循环将完成相当于字母数的迭代次数,因此计数将等于字母数。在这种情况下,无法找到打印,明白了!!谢谢…-)
found = False
for i in alphabets:
    if i == searchLetter:
        print("xyz")
        found = True
if not found:
    print("Letter not found")