Home>

I want to enter a number in n, enter a word from the keyboard according to the number, and then display',' between the words and'.' At the end of the sentence.
As a specific example
n = 2
America
India
When you enter

I want to output as follows.
America, India.

It may be difficult to understand with poor sentences, but please explain.

The word entered in a is stored, but since the word is decomposed and stored character by character,'.' Does not appear where I want to display'.'.

Corresponding source code python

Source code
n = int (input ())
a = ""
for i in range (n):
a + = input ()
if a == a [-1]:
a + = "."
else: else:
a + = ","
print (a)

Output result
America, India,

What I tried

To store the character string stored in a as one word
b = [a]
As a result
It became like ['A','m','m','r','i','c','a' ....'d','i','a']

Please provide more detailed information here.

  • Answer # 1

    Prepare a list in advance and add the words received by input () to the list.
    And''. Join (Sequence)It is possible to concatenate the character strings in the list with.
    It is also possible to separate them with a specified character string and concatenate them. If you want to separate them with commas, you can specify them.
    And if you want to put the end of the sentence in a periodprint (end ='.')You can output the end of the sentence with a period.

    n = int (input ())
    lst = []
    for i in range (n):
        lst.append (input ())
    print (','. join (lst), end ='.')

  • Answer # 2

    I want to change the process only the last time, so
    if a == a [-1]:
    Not
    if i == n-1:
    Probably.