[英]How do I have a user enter words line by line until the Enter key is pressed?
I had a homework assignment that required me to check each word in a list for a specific letter, and tell the user how many of the words in the list contained the letter. 我进行了一项作业,要求我检查列表中的每个单词是否包含特定字母,并告诉用户列表中有多少个单词包含该字母。 Below is my code for this: 下面是我的代码:
letterSearch = input("Please enter a letter to search for: ")
w = ['', '', '', '']
w[0] = input("Please enter up to 4 words: ")
w[1] = input(": ")
w[2] = input(": ")
w[3] = input(": ")
first = 0
second = 0
third = 0
fourth = 0
for ch in w[0]:
if ch is letterSearch:
first = 1
for ch in w[1]:
if ch is letterSearch:
second = 1
for ch in w[2]:
if ch is letterSearch:
third = 1
for ch in w[3]:
if ch is letterSearch:
fourth = 1
ans = first + second + third + fourth
print(ans, "of the entered words contain the letter", letterSearch)
The problem I was having was that in the homework, the professor wanted the user to be able to input words until the enter key was pressed, whereas I set the maximum amount of words that could be entered to 4. Is there a way to have the user be able to input any number of words, with the words still being separated in the list, until the enter key is pressed? 我遇到的问题是,在家庭作业中,教授希望用户能够输入单词,直到按下回车键为止,而我将可以输入的单词的最大数量设置为4。有没有办法用户能够输入任意数量的单词,而单词仍在列表中分开,直到按下回车键?
以单个字符串读取整个输入,然后使用split
将其分解为单词列表。
You can use split
on a string to divide it up into pieces. 您可以在字符串上使用split
来将其分成多个部分。 Just ask your user to separate the words provided to input
with some character: spaces are a convenient one. 只需要求您的用户将input
带有某些字符的单词分开即可:空格是一个方便的空格。 Then you can create a list from the inputted string and proceed as with the rest of your assignment. 然后,您可以根据输入的字符串创建一个列表,并按照其余的分配进行操作。
>>> w = input("Please enter words separated by spaces:")
Please enter words separated by spaces:one two three four five six
>>> w
'one two three four five six'
>>> w_list = w.split(" ")
>>> w_list
['one', 'two', 'three', 'four', 'five', 'six']
To split a string with any number of words (given that they are delimited by a space 用任意数量的单词分割字符串(假设它们之间用空格分隔) ), use the built-in
.split()
function like so: ),请使用内置的.split()
函数,如下所示:
my_str = "this sentence has some words"
print(my_str.split())
>>> ['this', 'sentence', 'has', 'some', 'words']
Aside from this, your code also has some factoring issues. 除此之外,您的代码还存在一些分解问题。 It's impractical for you to manually search each word for the letter, given that you will not know the number of words. 鉴于您不知道单词的数量,因此您无法手动在每个单词中搜索字母。 It would be better for you to iterate through the list of spliced words: 最好遍历拼接词列表:
letter_search = input("Please enter a letter to search for: ")
words = input("Please enter some words: ")
words = words.split()
in_word_count = 0
# Go through the list of words;
for word in words:
# Check if the letter specified is in the word
if letter_search in word:
# Increment the count
in_word_count += 1
print(in_word_count, "of the entered words contain the letter", letter_search)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.