简体   繁体   English

如何获取我(如果在列表中为“输入”)检查输入的每个字母,而不仅仅是输入的第一个字母?

[英]How do I get my (if “input” in list) to check every letter of the input and not just the first letter of input?

How do I get my (if "input" in list) to check every letter of the input and not just the first letter of input? 如何获得我(如果在列表中为“输入”)检查输入的每个字母,而不仅仅是输入的第一个字母?

This is my code now: 现在是我的代码:

alphabet = "abcdefghijklmnopqrstuvwxyzæø˚a ?"

my_list=list(alphabet)
n= input()



def textis():
for word in n.split():
    if word in my_list:
      print(word)
    else:
        x=word.replace(n,"?")
        print (x)



textis()

but it only checks the first letter of the input. 但它仅检查输入的首字母。 I want it to check every letter of input and change the ones that dont are in list to "?", and print the input again with the changes "?" 我希望它检查输入的每个字母,并将列表中不存在的字母更改为“?”,然后使用更改“?”再次打印输入。 in the sentence. 在句子中。 So if input is, hello My name Is, output should be hello ?y name ?s. 因此,如果输入为hello我的名字是,输出应为hello?y name?s。

Here are the problems: 这里是问题:

  • You need to pass in arguments in a function. 您需要在函数中传递参数。
  • You need to check every letter in the word instead of checking and changing the entire word. 您需要检查单词中的每个字母,而不是检查和更改整个单词。

What I did was make a new variable and made changes to that variable and then returned this variable. 我所做的是制作一个新变量,然后对该变量进行了更改,然后返回了该变量。

alphabet = "abcdefghijklmnopqrstuvwxyzæø˚a ?"

my_list = list(alphabet)
n = input()

def textis(n, my_list):
  new = ''
  for letter in n:
    if letter in my_list:
      new += letter
    else:
      new += "?"
  return new

print(textis(n, my_list))

Your function will work as is with a slight modification. 稍作修改,您的功能将照常工作。 when you n.split() you are generating a list of words, n.split() == ['hello', 'My', 'name', 'Is'] so when you iterate for word in n.split(): if word in my_list you are comparing words like name vs individual letters in my_list , you will never get a match. 当您使用n.split() ,会生成一个单词列表, n.split() == ['hello', 'My', 'name', 'Is']因此当您for word in n.split(): if word in my_list你比较喜欢的话name VS个别字母my_list ,你将永远不会得到匹配。 instead you have to use another level of nesting, for i in word , then it will work! 取而代之的是,您必须使用另一层嵌套, for i in word ,那么它将起作用! (Also there is no need to create a list out of alphabet for this case, you can use it as is) (在这种情况下,也无需alphabet创建列表,您可以按原样使用它)

alphabet = "abcdefghijklmnopqrstuvwxyzæø˚a ?"

n= input()

def textis():
    for word in n.split():
        for i in word:
            if i in alphabet:
                pass
            else:
                word = word.replace(i,"?")
        print(word) 
textis()

All this being your program is still returning all words on separate lines and a little over doing for the task, instead something like this would do the trick. 所有这些都成为您的程序,仍然会在单独的行上返回所有单词,并且为该任务做的工作有点过头了,相反,这种方法可以解决问题。

s = input('Enter a word: ')
for i in s:
    if i not in alphabet:
        s = s.replace(i, '?')
# hello ?y name ?is

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户输入的字母是否在一个单词中 - How do I check if a letter from user input is in a word 如何将用户输入与列表中的大写字母字符串进行比较? - How do I compare user input to an uppercase letter string in a list? 如何让python显示输入的第一个字母 - How to get python to display the first letter from input 如何检查输入是字母“l”、“h”还是“c” - How to check if the input is letter 'l', 'h' or 'c' Python如何检查输入是字母还是字符 - Python how to check if input is a letter or character 如何检查字符串输入中是否有某个字母,然后列出该字母所附的变量? - How to check if a string input has a certain letter and then list a variable that's attached to that letter? 有没有办法检查每个单词的首字母并根据 python 中的用户输入删除单词? - Is there a way to check the first letter of every word and delete a word based on user input in python? 如何检查用户给定输入的字符串是否等于 Python 中的某个字母/单词 - How do I check if a string with a user given input is equal to a certain letter/ word in Python 如何在Python中输入一个字母以输入输入 - How do I type one letter at a time for an input in Python 如何使用 if 语句进行以某个字母开头的输入 - How do I use the if statement for input starting with a certain letter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM