nutship
nutship

Reputation: 4924

basic input and the for loop

I'm just playing around with Python, very basic stuff.

The logic is as follows:

  1. User provides 3 Celsius temperatures,
  2. The body of the module count theirs Fahrenheit equivalents,
  3. And prints them out as output.

I want to use the for loop for this task.

def main():
    c1, c2, c3 = input("Provide 3 Celsius temps. separated with a comma: ")
    for i in range(c1, c2, c3):
        fahrenheit = (9.0 / 5.0) * i + 32
        print "The temperature is", fahrenheit, "degrees Fahrenheit."

main()

Well, the above code only transtaltes and prints the first Fahrenheit tempatarute that user has provided.

Some hints needed please.

Upvotes: 0

Views: 114

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122002

Remove the range() call altogether:

for i in (c1, c2, c3):

Now you are making (c1, c2, c3) a tuple, and you can loop over that directly. range() is only needed when you need to make a series of integers.

When print is given an expression with a trailing comma, it won't print a newline, so to get all three values on one line, one (simple) way to do that would be:

c1, c2, c3 = input("Provide 3 Celsius temps. separated with a comma: ")
print "The temperatures are", 
for i in range(c1, c2, c3):
    fahrenheit = (9.0 / 5.0) * i + 32
    print fahrenheit,
print "degrees Fahrenheit."

We can make this complicated fast, work through your tutorials a bit more and more powerful Python structures will be available soon enough. :-)

Upvotes: 4

Related Questions