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.
Here are the problems:
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. instead you have to use another level of nesting, for i in word
, then it will work! (Also there is no need to create a list out of alphabet
for this case, you can use it as is)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.