我正在制作一個用戶輸入一些文本的程序,并且我不想將該文本中不在變量“alfabet”中的所有字符更改為“?”。我怎樣才能做到這一點?我還想在兩個函數(shù)中完成它,main 和 clean_text。我的代碼現(xiàn)在看起來像這樣:def clean_text():for char in text:if char in alfabeth:continueelif char not in alfabeth:#don't know what to do here#text[] = "?"def main():userInput = input("type in text: ")text = list(userInput)if__name__ == "__main__":main()clean_text(text)
2 回答

炎炎設計
TA貢獻1808條經(jīng)驗 獲得超4個贊
您必須返回一些東西才能將其分配給user_input要傳遞到的變量clean_text()。這是您項目的工作版本。這也將處理由空格分隔的多個單詞。
def clean_text(word):
alphabeth = 'vshthstmpd'
for idx, item in enumerate(word):
if item == ' ':
pass
elif item not in alphabeth:
word[idx] = '?'
return ''.join(word)
def main():
user_input = input('Type in text: ')
text = list(user_input)
return text
if __name__ == '__main__':
text = main()
print(clean_text(text))
Type in text: vash the stampede
v?sh th? st?mp?d?
添加回答
舉報
0/150
提交
取消