简体   繁体   English

我如何让用户逐行输入单词,直到按下Enter键?

[英]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.

相关问题 按Enter键时如何将行编辑值分配给变量 - how to assign line edit value to a variable when enter key is pressed 如何在 Python 中编写一个 for 循环以反复要求用户输入一个数字,直到他们输入一个整数(0、1、2 等)? - How do I write a for loop in Python to repeatedly ask the user to enter a number until they enter in a whole number (0, 1, 2, etc)? 在 guizero 应用程序中按下 Enter 键时如何调用函数? - How to have a function called when Enter key is pressed in a guizero application? 如何知道用户是否使用 Python 按下了 Enter 键 - How to know if a user has pressed the Enter key using Python “在读行时”假定按下了回车键,并且仅执行一次迭代,但是“在行中”则适当地起作用 - 'while read line' assume enter key is pressed and only executes one iteration, but 'for line in blah' works approropriately 在将文本输入文本小部件后按Enter键时如何执行回调? - How do I execute a callback when the Enter key is pressed after entering text into a Text widget? 在python中按下键盘上的“ enter”键时,如何设置特定的动作 - How do I set a specific action to happen when the “enter” key on my keyboard is pressed in python 如果循环中未提供输入(或按下回车键),如何在 python 中结束无限循环 - How do I end an infinite loop in python if no input(or the enter key is pressed) is provided in the loop 如何在用户单击回车键之前暂停代码? - How to make the code pause until the user clicks the enter key? 如果在 kivy python 中按下一个键(回车),则执行一个函数 - Do a function if a key(enter) is pressed in kivy python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM