0

Not sure how to word my question. I'm trying to collect data with input in a for loop and append it every time. I want it to look like this: Each loop of input, I want to enter: SOL $XXX $ZZZ, BTC $XXX $ZZZ, ETH $XXX $ZZZ, etc.

And I want the finished output to look like this:

* `BTC 4/12@07:59pm :: $291.42 @ $40,173.45`
* `SOL 4/12@07:59pm :: $267.02 @ $103.28`
* `LTC 4/12@07:59pm :: $220.91 @ $104.82`
* `ETH 4/12@07:59pm :: $177.43 @ $3,031.23`
* `wETH 4/12@07:59pm :: $45.20 @ $3,031.23`
* `Total 4/12@07:59pm :: $1,001.98`

I'll be able to format it, I just can't figure out how to keep appending the input. I'm sure it'll be something simple that I just can't picture at the moment. Hopefully I worded this well enough for someone to understand my query.
Thanks in advance.

2
  • are you talking about string? why don't you concatenate strings in your for loop? Commented Apr 13, 2022 at 1:59
  • No. I want every string I supply with input to keep appending the final output. Basically like this: var=input; var+=var or var.append(var) and have the final output look like it does in the OP. But I'm having difficulty with picturing how it should go. Commented Apr 13, 2022 at 2:22

1 Answer 1

1

So we have 2 issues here. 1 making the for loop, 2 printing stuff out.

For 1 we'll just do a while loop (since we don't know when you'll stop) based on the input:

from datetime import datetime
user_input = "not empty"
inputs_list = []
while True:
  user_input = input("What are your coordinates?")
  if user_input == "":
    break
  split_string = user_input.split(" ", 1)
  string_with_date = datetime.now().strftime(
    "{prefix} %m/%d@%I:%M%p {coordinates}".format(prefix=split_string[0], coordinates=split_string[1])
  )
  inputs_list.append(string_with_date)

This will keep looping until you give an empty return to the input call. Then we can print all of your results with:

for row in inputs_list:
  print("* `{row}`".format(row=row))

The heavy lifting is done by this part:

string_with_date = datetime.now().strftime(
    "{prefix} %m/%d@%I:%M%p {coordinates}".format(prefix=split_string[0], coordinates=split_string[1])
  )

We use the split method to cut your inputs at the first space and glue them back together in the strftime argument with the date in between.

For more info on strftime or on split, check the documentation linked in this message.

Sign up to request clarification or add additional context in comments.

7 Comments

This is pretty much exactly what I'm looking for, I'm just having trouble with the formatting on it. Doing {row[0]} {row[1]} etc isn't working. Tried adding split to input but that's not working. Thank you very much.
I got formatting to work on the string_with_date bit but now it's printing it one line at a time as I enter the input. Printing it in the for loop at the bottom is giving me trouble with formatting.
What is the issue with the formatting? Did you want it to print one at a time or all at the end?
Nothing wrong with formatting anymore. Only problem is it's only one or the other. Using print in your for loop that works but doesn't format it properly. Or I can use the string_with_date you wrote but it doesn't print it all at once. It prints every time I give input.
I combined both in the first snippet. Look at the last few lines. I append all the string_with_date results to a list and then I print everything in the list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.