1

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. 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? the second program is as below:

import sys
text=sys.stdin.readlines()
for esm in text:
    x=esm.lower().capitalize()
    print(x)
Annahita
  • 47
  • 4
  • Can you use multiple inputs and add them to a string? – The Thonnu Aug 23 '22 at 13:17
  • What does: `take 10 names as the input` mean? What does the user have to type? – quamrana Aug 23 '22 at 13:18
  • 1
    The "without lists" restriction is weird and inconsistent with both your implementations already. `text.split()` produces a list. So does `sys.stdin.readlines()`. – Daniil Fajnberg Aug 23 '22 at 13:22
  • 2
    if you know the number of names to be introduced (in your case 10) just make a for loop that iterates 10 times `for _ in range(10):` If you want the number of names to be chosen by the user who inputs them, you could make an infinite loop that just reads one line and checks if it's empty to break the loop, this way you can finish introducing names by pressing enter twice (which creates an empty line) –  Aug 23 '22 at 13:22
  • The user has to type 10 names. – Annahita Aug 23 '22 at 13:24
  • 1
    Please update your question with a sample session of this program you want. (You could shorten it to the user entering 2 names and the program printing 2 names). – quamrana Aug 23 '22 at 13:25

5 Answers5

2

If it's 10 the number of lines to be introduced then use a for loop

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)

.title makes a string lowercase except for the first character which will be uppercase

  • Thank you for help. But I forgot to mention that the program is to first take all the 10 names and then print them in vertical from in .title() – Annahita Aug 23 '22 at 13:39
  • if you need to first take the 10 names then you will need a list to store them. But you mentioned without using lists. For the next time an example of the input and desired output would be very helpful. –  Aug 23 '22 at 14:22
  • @SembeiNorimaki you could store in a string and then print as shown in my response below – SanV Aug 23 '22 at 18:21
  • Yes, but that's a hacky solution. Instead of hacky solutions it's better to just wait until the lists are introduced and use them. Using a string and separating by spaces might have some problems: What if your name is a compound name separated by space? –  Aug 24 '22 at 09:46
1

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='')
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

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()
Daniil Fajnberg
  • 12,753
  • 2
  • 10
  • 41
  • I want to do it without list as it is a project I have to do for my python course and it hasn't taught us lists yet! But we can use the methods that we find on the internet, even if they turn the string into a list :) And I know that it's weird :) – Annahita Aug 23 '22 at 13:31
0

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
SanV
  • 855
  • 8
  • 16
-1

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())
eisa.exe
  • 78
  • 9
  • correct, i have edited the answer to be more wellsuited towards the question – eisa.exe Aug 23 '22 at 13:38
  • text.split() returns a list so that's not a viable solution. Another problem would arise if the name included whatever character(s) were assigned to SEPARATOR – DarkKnight Aug 23 '22 at 13:52
  • well a name was the input and names are very likely not going to include a specific separator chosen which is hardcoded and in the question text.split() is used as an attempted solution. Lists are viable as long as the @Annahita understands the usage (as they specified the reason comprised their not knowing how to use lists) – eisa.exe Aug 23 '22 at 20:22