Python Forum
Using a range and for loop - temp converter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a range and for loop - temp converter
#1
Hello,

     This is my first post and my first exposure to Python as a result of a college course I'm taking. My assignment asks to modify a program so that is computes and prints a table of celsius temperatures and the Fahrenheit equivalent every 10 degrees from 0C to 100C. The textbook is not very clear as to how I am supposed to work such a loop, and I've tried many different ways in the last hour but I can't quite figure out if I am defining my variables in the wrong spot or if I am not properly defining the loop. I keep getting a "SyntaxError: Missing parentheses in call to 'print'", so I hope I can get some direction here.

Thanks!

def main():
   print("This program computes and prints a table")
   print("of Celsius temperatures and the Fahrenheit equivalent")
   print("every 10 degrees from 0C to 100C")

   celsius = eval(input("Enter the number zero to begin "))

   for i in range (0, 101, 10):
       fahrenheit = 9/5 * celsius + 32
   
   print("The temperature is", fahrenheit, "degrees Fahrenheit.")

main()
I'm not getting an error per se, but rather this response:

This program computes and prints a table

of Celsius temperatures and the Fahrenheit equivalent
every 10 degrees from 0C to 100C
Enter the number zero to begin 0
The temperature is 32.0 degrees Fahrenheit.

Process finished with exit code 0

I need it to create a table from 0C to 100C, so I don't understand how to get the loop to continue on and create my table.
Reply
#2
this line
print("The temperature is", fahrenheit, "degrees Fahrenheit.")
is not part of the loop, so it prints only last temperature. Correct the indentation and it will work as expected.
Reply
#3
Ok, so now I fixed it, but it's generating 10 results of 32.0 degrees F (which is 0C)

def main():
    print("This program computes and prints a table")
    print("of Celsius temperatures and the Fahrenheit equivalent")
    print("every 10 degrees from 0C to 100C")

    celsius = eval(input("Enter the number zero to begin "))

    for i in range (0, 101, 10):
        fahrenheit = 9/5 * celsius + 32
        print("The temperature is", fahrenheit, "degrees Fahrenheit.")

main()
Here is the result:
This program computes and prints a table
of Celsius temperatures and the Fahrenheit equivalent
every 10 degrees from 0C to 100C
Enter the number zero to begin 0
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.
The temperature is 32.0 degrees Fahrenheit.

Process finished with exit code 0
Reply
#4
you need to use the name from the loop in the calculations:

def main():
   print("This program computes and prints a table")
   print("of Celsius temperatures and the Fahrenheit equivalent")
   print("every 10 degrees from 0C to 100C")

   input("Press key to begin..."))

   for celsius in range (0, 101, 10):
       fahrenheit = 9/5 * celsius + 32
       print("The temperature is", fahrenheit, "degrees Fahrenheit.")

main()
Reply
#5
Got it, thanks. Obvious mistakes, but the Zelle book does a poor job of explaining the loops. Now I have to figure out how to make the result say something like "0 degrees C equals 32 degrees F" and so on all the way to 100C. I'll try to figure that out and post if I have issues. Right now it just goes down the list saying "The temperature is X degrees Fahrenheit" from 0C to 100C.
Reply
#6
You could use the "print" format to pretty much do what you you'd like.  For instance you could have this:

for celsius in range(0, 101, 10):
    print("The temperature is {} degrees Fahrenheit.".format(9/5 * celsius + 32))
Notice the use of the double "{}", this corresponds to the first (and currently only) argument in the .format()
That should give you a clue as to how to additionally add content to your print function
For a complete description, refer here: https://docs.python.org/3/tutorial/inputoutput.html
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My first temperature converter TheLowEndTheory 7 3,338 Oct-18-2020, 04:39 PM
Last Post: buran
  How can I run a function inside a loop every 24 values of the loop iteration range? mcva 1 2,101 Sep-18-2019, 04:50 PM
Last Post: buran
  Temperature converter Help (ASAP) Edison_Weng 1 2,792 Apr-16-2018, 01:55 PM
Last Post: stranac
  Debugging error, while loop out of range zenort 1 3,935 Feb-14-2018, 03:36 PM
Last Post: zenort
  Temp Converter with Kelvin vader33 7 12,053 Oct-02-2016, 05:16 AM
Last Post: vader33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020