How Can I Improve My Print Statement in Python?

0
10
Asked By CoolCoder93 On

Hi everyone! I'm just starting out with Python and I'm having a bit of trouble with a homework task that involves calculating some running times. The task goes like this: If I leave my house at 6:52 AM and run 1 kilometer at an easy pace of 8:15 per kilometer, followed by 3 kilometers at a tempo of 7:12 per kilometer, and finish with another 1 kilometer at an easy pace, what time will I get home for breakfast?

I've managed to compute the hours, minutes, and seconds correctly, but I'm not happy with how the output looks. Here's my code where I'm currently using the print function:

```python
print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":",seconds_conv2)
```

The printed result is "Breakfast start time would be: 7 : 30 : 6", but I want to improve the formatting. Any suggestions on how to adjust this? Here's the full code I'm working with:

# Starting Conversion
start = 6*3600 + 52*60
start_conv = 0

# Formula:
start += (8*60 + 15)
start += (3*(7*60 + 12))
start += (8*60 + 15)

# Hours:
hours_conv = start // 3600

# Minutes:
minutes_conv1 = start % 3600
minutes_conv2 = minutes_conv1 // 60

# Seconds:
seconds_conv1 = start % 3600
seconds_conv2 = seconds_conv1 % 60

# Final Results:
print(hours_conv)
print(minutes_conv2)
print(seconds_conv2)

# Final Results:
print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":",seconds_conv2)

5 Answers

Answered By TechieGal88 On

You might want to check out f-strings if you haven't already! They’re a fantastic way to handle string formatting in Python. You could also use `str()` to convert numbers to strings and concatenate them, but f-strings are definitely more elegant!

CuriousRunner21 -

I appreciate the tip! I’ll look into f-strings right away and see how I can fix my print output. Thanks a bunch!

Answered By PrintMaster99 On

You’ve got a couple of options! By default, `print()` separates different arguments with a space, which is why you’re seeing those gaps. You can use:

```python
print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":", seconds_conv2, sep="")
```

Or just use f-strings directly for better control:

```python
print(f"Breakfast start time would be {hours_conv}:{minutes_conv2}:{seconds_conv2}")
```

AppreciativeNewbie -

Thanks a lot! I’m trying it out as we speak!

Answered By CodeExplorer57 On

It looks like your calculation logic is solid! The issue really comes down to how you format the output. Just using f-strings or adjusting the `sep` should sort that out!

FeedbackHunter -

Thanks for the encouragement! I struggled to find practical advice on other forums, so I really appreciate it. My formatting is fixed now!

Answered By ThirtyMinuteChef On

Why not utilize the `datetime` module for simpler handling? Here’s a quick example:

```python
from datetime import datetime, timedelta

start = datetime.strptime("06:52", "%H:%M")
end = start + timedelta(seconds=60*60 + 15)
answer = end.strftime("%H:%M:%S")
print(f"Breakfast start time would be {answer}")
```

Answered By DataNinja42 On

If you look at the documentation for the `print` function, you'll see it uses a space as the default separator. You can change that with the `sep` argument. Try doing this:

```python
print("Breakfast start time would be", hours_conv, ":", minutes_conv2, ":", seconds_conv2, sep="")
```

Alternatively, you could combine your strings into one and print it out, like this:

```python
message = f"Breakfast start time would be {hours_conv}:{minutes_conv2}:{seconds_conv2}"
print(message)
```

HelpSeeker45 -

Thanks for pointing that out! I did check the docs but didn’t realize the `sep` option. I tried using an f-string afterwards and it worked great!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.