简体   繁体   English

如何在不使用列表的情况下在 python 中进行多行输入?

[英]How do I make a multi-line input in python without using a list?

I have to write a program without using list(as I still haven't learnt them) to take 10 names as the input and put them all in lower-case and then capitalize the first letter.我必须在不使用列表的情况下编写一个程序(因为我还没有学会它们)以将 10 个名称作为输入并将它们全部小写,然后将第一个字母大写。 I came up with two programs that both work;我想出了两个都可以工作的程序; However, there are some problem: the first programs is:但是,有一些问题:第一个程序是:

text=input()
x=text.split()
for name in x:
    x=name.lower().capitalize()
    print(x)

in the above program I can only take the names in one line and if the user presses enter in order to type the new name in a new line the program would think that the input is over and all.在上面的程序中,我只能在一行中输入名称,如果用户按下回车键以便在新行中输入新名称,程序会认为输入已经结束。 so I searched the internet and found readlines() method but I have no idea how to put a limit to the inputs and I do it manually with "ctrl+z", is there a way I can put a limit to it?所以我搜索了互联网并找到了 readlines() 方法,但我不知道如何限制输入,我用“ctrl+z”手动完成,有没有办法可以限制它? the second program is as below:第二个程序如下:

import sys
text=sys.stdin.readlines()
for esm in text:
    x=esm.lower().capitalize()
    print(x)

If it's 10 the number of lines to be introduced then use a for loop如果要引入的行数为 10,则使用 for 循环

for _ in range(10):
    name = input().title()
    print(name)

the _ in the loop means you don;t need to keep the variable (you just need to iterate 10 times)循环中的 _ 意味着您不需要保留变量(您只需要迭代 10 次)

.title makes a string lowercase except for the first character which will be uppercase .title使字符串变为小写,但第一个字符为大写

Using a list would be the natural way but as that's a constraint you could do it like this:使用列表将是自然的方式,但由于这是一个约束,你可以这样做:

t = ''

for i in range(10):
    name = input(f'Input name {i+1}: ')
    t += name + '\n'

print(t, end='')

It is still not entirely clear what exactly you want to do and why you say that you want to do it "without lists" but show examples of you doing it with lists.仍然不完全清楚您到底想做什么以及为什么说要“不使用列表”进行操作,但显示了您使用列表进行操作的示例。 Anyway, here is a guess as to what you might be looking for:无论如何,这里是您可能正在寻找的猜测:

def main():
    string = input("Type in names (separated with spaces): ")
    for name in string.split():
        print(name.title())


if __name__ == '__main__':
    main()

You could do something like this:你可以这样做:

n = 3
name = ''
print(f'INPUT {n} NAMES:')
for i in range(n):
    name += input(f'{i+1}:  ').title() + '\n'
print(f'OUTPUT:\n{name}')

INPUT 3 NAMES:
1:  zALL
2:  WALL
3:  FALL
OUTPUT:    
Zall
Wall
Fall

All answers are probably viable however in the case that there is no name you don't want to add an empty name to your text do you?所有答案都可能是可行的,但是在没有名称的情况下,您不想在文本中添加空名称,对吗? In that case you need to do it like this:在这种情况下,您需要这样做:

text = ""
i = 0
while i < 10:
    x = input()
    if x != "":
        text += x + SEPARATOR # § is the separator, you can use anything
        i += 1

for t in text.split(SEPARATOR):
    print(t.title())

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM