Python Loop for temperature

Question:

I am learning python for the first time and my assignment required me to:

Write a loop to print all elements in hourly_temperature. Separate
elements with a -> surrounded by spaces. Sample output for the given
program:

90 -> 92 -> 94 -> 95 

Note: 95 is followed by a space, then a newline.

After playing I had difficulty and found the coded answer as:

hourly_temperature = [90, 92, 94, 95]

Htemp = len(hourly_temperature)

#create list
temps = []

if Htemp >= 0:
    for i in hourly_temperature: #question on i and appends i
        temps.append(i)
        temps.append('->')
        Htemp -= 1
        continue
temps.pop()
for s in temps:    #question here
    print(s,'', end='')
    continue
print('')

My question is regarding the for loops that I have notated with the #question.

I am wondering what the purpose is for the i and s to make the code work.

Asked By: Alita

||

Answers:

i takes each value of hourly_temperature, as s takes the value of each element in temps.

This is a very complicated way to join values with a separator. Better to use str.join:

hourly_temperature = [90, 92, 94, 95]
print(' -> '.join(map(str, hourly_temperature)))
Answered By: Daniel

The i and the s are going to be variables that represent the next value in the list every time the loop iterates. You need them to actually access each item.

You should try to rewrite your answer to only use one loop and avoid creating the second list temps

You should also be able to do this without using Htemp, pop(), or continue.

Answered By: C-RAD

I am sure many people here will give you ‘Pythonic’ one-liners that will output what you need. But I think you’re at the stage where you need to learn how to code logically, rather than how to be Pythonic.

The example you give is messy and not elegant in any language.

Here is how I would code your assignment, using simple logic suitable for a beginner. Hopefully this will be more helpful than a short one-liner.

hourly_temperature = [90, 92, 94, 95]

# Start with an empty string
s = ""

# Step through each temperature in the list
for t in hourly_temperature:

    # If the string contains a number, then we need to add ->
    # before the next number.
    if s != "":
        s += "-> "

    # Add the temperature and a space
    s += str(t) + " "

# Print the string we have just built
print(s)

Another common way to achieve the result is to not worry about checking whether “-> ” needs to be prefixed during the loop. Instead, always prefix “-> ” and remove the erroneous one at the end. This is slightly more efficient for large loops.

hourly_temperature = [90, 92, 94, 95]

s = ""
for t in hourly_temperature:
    s += " -> " + str(t)

# At this stage, s contains " -> 90 -> 92 -> 94 -> 95" so we need to remove the first ->
# We do this by keeping everything from character 4 onwards
print(s[4:])
Answered By: Thane Brooker

Your example is very messy and can be made very small
For Example:

hourly_temperature = [90, 92, 94, 95]
joined_temps = " -> ".join(map(str, hourly_temperature)) + " " # Add the temperatures together with " -> " as separaters and add the space as you said
print(joined_temps)

"separater".join(typing.Union[list, tuple]) joins a list or tuple together separated by the given separater, then we add the space as you said

Then we print it because print already provides a newline at the end of the string

If you would like it a for loop form

hourly_temperature = [90, 92, 94, 95]
ordered_temps = []
for temp in hourly_temperature:
    ordered_temps.append(temp)
    if not temp == hourly_temperature[-1]:
        ordered_temps.append(" -> ")
    else:
        ordered_temps.append(" ")
print("".join(map(str, ordered_temps)))

We are once using join again but this has no separater, so it will only join the list.

Answered By: Malik Tahir