``````# convert2.py
# PPython Monty
# Lab 2 Ch 2, PE 4
# A program to convert temperatures from Celsius to Fahrenheit and
# vice versa

def main():
# description of program
print("This program converts Celsius temperatures to Fahrenheit and")
print("vice versa.")
conversion = ("C")
n = eval(input("How many temperatures would you like converted?: "))
for i in range(n):
conversion = input("Enter 'F' to convert to Fahrenheit, 'C' to convert to Celsius or a number: ")
if conversion == ("F"):
F = 9/5 * C + 32
elif conversion == "C":
C = (F - 32) * 5/9
else:
C = (F - 32) * 5/9

main()
``````

Basically I'm suppose to ask the user to input how many times they want to run the loop, then after they input a number, depending on if they enter C or F, the number they input next would be converted to Celsius or Fahrenheit.

I'm a noob at this, so I don't know exactly what I am doing wrong here. The code looks right and runs, but as soon as I enter F or C, it crashes. Also once I fix that problem, I'm not even sure if the code would work once I enter a number to get converted.

The thing is I keep getting errors no matter what I do. If I enter C or F, I get
"UnboundLocalError: local variable 'F' referenced before assignment"
or
UnboundLocalError: local variable 'C' referenced before assignment

## All 8 Replies

At line 16, your program is using a value C which was not defined before. At line 18 and 20, you're using a value F which was not defined before.

How should/Do I define C?

If you choose to convert to Farenheit for example, you must also enter a value to convert.

Can someone guide me or help me? I don't know how to implement all of these suggestions.
I've only been doing Python for about 2 weeks.

You must start from simple functioning program and build on it, here little advanced

``````# make code work in both Python2 and Python3
from __future__ import print_function, division
try:
input = raw_input
except:
#Using Python3
pass

conversion = input("Enter 'F' to convert to Fahrenheit, 'C' to convert to Celsius: ").upper()
try:
if conversion == "F":
print(9/5 * float(input('Give Celcius: ')) + 32, 'Fahrenheit')
elif conversion == "C":
print((float(input('Give Fahrenheit: ')) - 32) * 5/9, 'Celcius')
else:
raise ValueError('Give F or C')
except Exception as e:
``````

• Using the for loop and the if statement.
• Using int’s, float’s, and variables in expressions to compute Fahrenheit from Celsius
• Using input() to read numbers from the shell into variables of type string
• Using eval() to convert the inputted numbers to variables of type int or float

• Using print() to format the output

• Initialize the variable conversion to “C” using the following assignment statement.
conversion = “C”
• Determine the number of times to loop (N) by asking the user.
• Use a for loop to loop the user defined number of times, allowing a different temperature or the
letters “F”or “C” to be entered on each iteration of the loop.
• Use input() to assign the user’s entered temperature to a variable of type string.
• Before using eval() to convert the user’s entered temperature, use an if-elif-else statement to
determine if the user entered one of the following: “F”, “C”, or a number.
• If the user enters “F”, then assign “F” to the variable conversion using the following
statement.
conversion = “F”
• Else if the user enters “C”, the assign “C” to the variable conversion using the following
statement.
conversion = “C”
• Else convert the number that was entered based upon the value that is in the variable
conversion. If the variable conversion has an “F”, convert the user’s number to Fahrenheit.
If the variable conversion has a “C”, convert the user’s entered number to Celsius.
• For all numbers that are converted, display the iteration number, temperature in degrees Celsius,
and temperature in degrees Fahrenheit.
• Place comments after def main(): that declare the types of your variables. For examples,

int i
float f, c
string conversion

The thing is I haven't learned those advanced techniques. I am pretty much suppose to only use the coding I have learned so far. I thought that I have the majority of the code written for this program, but I am just stuck because of my errors.

How do I define the value C, and the Value F? Also after I ask them to input C or F, how do I get them to input a number to get converted?

I would surely not use eval for user input, it is very dangerous as you could for example delete all files from the hard disk or something equally funny!

The logic given to you that allows conversion type to be changed instead of inputing number, does not work with for loop, as you would spend one turn for conversion type change. Didn't you learn while loop yet?

``````# convert2.py
# PPython Monty
# Lab 2 Ch 2, PE 4
# A program to convert temperatures from Celsius to Fahrenheit and
# vice versa

def main():
print("This program converts Celsius temperatures to Fahrenheit and")
print("vice versa.")
conversion = "C"
n = int(input("How many temperatures would you like converted?: "))
for turn in range(n):
c = input("""Enter 'F' to convert to Fahrenheit,
'C' to convert to Celsius or
a number (converting to {}): """.format(conversion))
if c=='F':
conversion = 'F'
continue
elif c=='C':
conversion = 'C'
continue
elif conversion == "F":
result = 9.0/5.0 * float(c) + 32
elif conversion == "C":
result = (float(c) - 32) * 5.0/9.0

print('{}: {:.2f} {}'.format(turn+1, result, conversion))

main()
``````

It might be simpler to have the user enter the Fahrenheit value as 32F. Your program finds the 'F' and knows that the result has to be Celsius. Conversely for Celsius values entered ......

``````ts = raw_input("Enter temperature like 32F or 36C : ").upper()

if 'F' in ts:
tf = float(ts.replace('F', ''))
cel = (tf - 32) * 5.0/9.0
print("%0.2f Fahrenheit = %0.2f Celsius" % (tf, cel))
elif 'C' in ts:
tc = float(ts.replace('C', ''))
fah = 9.0/5.0 * tc + 32
print("%0.2f Celsius = %0.2f Fahrenheit" % (tc, fah))
else:
print("Sorry, didn't understand %s" % ts)
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.