How to request input data N times, and concatenate this data

Asked

Viewed 599 times

1

First I asked the user to enter the amount of links that he wanted to add to the file (in this case a string), hence if for example he type 4, I want to ask the question "Download link: " four times to it and add all the answers to that question in a string. How do I do that?

Code:

c=input("Quantos links de download deseja colocar: ")
print("")
dw=input("Link de Download: ") 

Then he takes every answer and adds to a string end as one string.

2 answers

3

You could use the for because in case after the user type the amount of times he wants to insert the links you will know how many times you have to repeat the loop, then just use lists to store the entered links. For example:

links = []
quant_links = int(input('Quantidade de Links: '))

for i in range(quant_links):
    link = raw_input('URL do link: ')
    links.append(link)

Then just treat the inserted links.

2


To make the loop is simple.

c = input("Quantos links de download deseja colocar: ")
print()
i = 0
texto = ""
while (i < int(c)):
    dw = input("Link de Download:")
    texto = texto + dw + "  "
    i = i + 1
    print()
print(texto)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Of course this implementation is well simplified, it will not do anything very useful, but from there you can get better as your need.

  • Perfect bro, I just need to make some adjustments, but this code was excellent, because I understood everything. I’m just going to adapt to the software.. It was really worth

  • Returning to the problem, the bad thing is that it keeps reusing the variable "text" and then records only the last link typed and how do I add this generated string at the end of another string? I’ll put my code in another comment, by organization

  • I’m using link = "[hide][url=" + dw + "][b]GSMFans-"+ nm + " - Link" + str(i) + "[/b][/url][/hide]" to generate the link.. so how to add this to another string, and how to not lose the previous links? I can’t use lists because [] and commas will get in the way

  • No, it records everyone in the string. It’s not very organized but it’s what I said, you’ll have to adapt to what you need. I’m even going to change a little because there was a little mistake. I’ll give you a hint, when you ask a question, put all the important details in it. Now you’re talking about other things that weren’t in the question. Anyway, I can try to help a little more if I understand what your problem is.

  • Ah, sorry I understood, in my code I had not put text = text ... or text += ... so I only recorded the last one.. So another question (which I didn’t make very clear in the topic) is how to add the contents of this string to another string that already has content.. I had already seen a code about it, but forgot how and where

  • This is the way I used it in the code. You’ll concatenate whatever you want with the +, when you don’t want to lose existing content, you just use the variable itself that you want to preserve the content as part of the new result, as I used in the code above. Not to lose the content of texto, I used this variable in the concatenation, thus the new value of texto will include what you already had in texto before.

  • Our kk gave a shave here, is that it is so much (study C, Javascript and Shell also) hence a confusion sometimes.. Thanks there

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.