-2

I got a confusing problem with my temperature converting program in Python, confusing for me at least since I'm new to this. I got two locations, Germany and USA, one country that the user is from and where the user is currently at. I'm just trying to convert the temperature from the temperature scale in the country the user is currently in, to the temperature scale to the country that the user is coming from.

For example, the user is from Germany but currently in the USA. So in that case I want the program to take the temperature the user is typing in to be converted from Celsius to Fahrenheit.

My code:

location = input("Where are you from?")

us = ("USA")
ger = ("Germany")

if location == ger:
print("You are from Germany")
elif location == us:
print("You are from the USA")
else:
print("Enter the country Germany or USA")

recentLoc = input("What is your location right now?")

if recentLoc == ger:
print("You are in Germany right now")
elif recentLoc == us:
print("You are in the USA right now")
else:
print("Please enter the country Germany or the USA")

temp = input("What is the temperature outdoor tomorrow?")

def convert_f():
f = float(fahrenheit)
f = (temp*9/5)+32
return(f)

def convert_c():
c = float(celsius)
c = (temp-32)*5/9
return(c)

if recentLoc == ger and location == us:
print("Temperature for tomorrow is " + float(c) + "Celsius or " + float(f) + "Fahrenheit")
elif recentLoc == us and location == ger:
print("Temperature for tomorrow is " + float(f) + "Fahrenheit or " + float(c) + "Celsius")
elif recentLoc == us and location == us:
print("Temperature for tomorrow is " + float(f) + "Fahrenheit")
elif recentLoc == ger and location == ger:
print("Temperature for tomorrow is " + float(c) + "Celsius")
else:
print("Please type in a number")

Error message:

NameError: name 'f' is not defined

4 Answers4

0

your define statement hasn't been run, however aren't needed simply exchange

def convert_f():
    f = float(fahrenheit)
    f = (temp*9/5)+32
    return(f)

def convert_c():
    c = float(celsius)
    c = (temp-32)*5/9
    return(c)

for

f = (temp*9/5)+32
c = (temp-32)*5/9
jack
  • 90
  • 1
  • 8
0

There were several errors in your code. Here is a working solution. I am not showing the initial portion of your code which I did not touch.

# User input here
# if else statements here

recentLoc = input("What is your location right now?")

temp = float(input("What is the temperature outdoor tomorrow?"))

def convert_f(temp): # The function modified
    f = (temp*9/5)+32
    return(str(f))

def convert_c(temp): # The function modified
    c = (temp-32)*5/9 
    return(str(c))

if recentLoc == ger and location == us:
    print("Temperature for tomorrow is " + convert_c(temp) + "Celsius or " + convert_f(temp) + "Fahrenheit")
elif recentLoc == us and location == ger:
    print("Temperature for tomorrow is " + convert_f(temp) + "Fahrenheit or " + convert_c(temp) + "Celsius")
elif recentLoc == us and location == us:
    print("Temperature for tomorrow is " + convert_f(temp) + "Fahrenheit")
elif recentLoc == ger and location == ger:
    print("Temperature for tomorrow is " + convert_c(temp) + "Celsius")
else:
    print("Please type in a number")
Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

You only defined the conversion functions but you did not call them.

location = input("Where are you from?")

us = ("USA")
ger = ("Germany")

if location == ger:
    print("You are from Germany")
elif location == us:
    print("You are from the USA")
else:
    print("Enter the country Germany or USA")

recentLoc = input("What is your location right now?")

if recentLoc == ger:
    print("You are in Germany right now")
elif recentLoc == us:
    print("You are in the USA right now")
else:
    print("Please enter the country Germany or the USA")

temp = input("What is the temperature outdoor tomorrow?")

def convert_f(temp):
    temp = float(temp)
    f = (temp*9/5)+32
    return(f)

def convert_c(temp):
    temp = float(temp)
    c = (temp-32)*5/9
    return(c)

if recentLoc == ger and location == us:
    print("Temperature for tomorrow is " + temp + "Celsius or " + str(convert_f(temp)) + " Fahrenheit")
elif recentLoc == us and location == ger:
    print("Temperature for tomorrow is " + temp + "Fahrenheit or " + str(convert_c(temp)) + " Celsius")
elif recentLoc == us and location == us:
    print("Temperature for tomorrow is " + temp + "Fahrenheit")
elif recentLoc == ger and location == ger:
    print("Temperature for tomorrow is " + temp + "Celsius")
else:
    print("Please type in a number")
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

You have a few bugs in your code:

  1. The two functions you defined convert_f and convert_c can't use fahrenheit or celsius, because you did not define them anywhere. My guess is you want to provide these values as paramters.
def convert_f(fahrenheit):
    f = float(fahrenheit)
    f = (f*9/5)+32
    return(f)

def convert_c(celsius):
    c = float(celsius)
    c = (c-32)*5/9
    return(c)
  1. In your last few lines you use the names of the return values of convert_f and convert_c. These are for one never created, because you never call the functions and even if they were called can't be accessed like that. The name of the return value loses all meaning outside the function. What you could do is something like that:
temp = float(temp)

if recentLoc == ger and location == us:
    print("Temperature for tomorrow is {:.2f} Celsius or {:.2f} Fahrenheit".format(temp, convert_f(temp)))
elif recentLoc == us and location == ger:
    print("Temperature for tomorrow is {:.2f} Fahrenheit or {:.2f} Celsius".format(temp, convert_c(temp)))
elif recentLoc == us and location == us:
    print("Temperature for tomorrow is {:.2f} Fahrenheit".format(temp))
elif recentLoc == ger and location == ger:
    print("Temperature for tomorrow is {:.2f} Celsius".format(temp))
else:
    # Technicaly this is printed when either recentLoc or location are neither ger or us
    print("Please type in a number")    

I use temp and the output of either convert_f and convert_c to print the output. Additionally you can't add a string and a float. You could either convert the float to string via str() e.g: "This is a float " + str(float(5)) + "!". This is a bit hacky and not considered great code. In the code above I used the format() function which not only gives you clearer, more readable code, it can also do some formatting e.g in the code above only 2 points of precision are given for any floating point number and not all that are calculated.

  1. The questions at the beginning of the code are a bit broken. You correctly check if the input is either Germany or USA and print an error message if that is not the case, but you don't repeat your question afterwards. I suggest using a simple while loop and to use break when you get a correct answer.
location = ""

while location != us and location != ger:
    location = input("Where are you from?")

    if location == ger:
        print("You are from Germany")
        break
    elif location == us:
        print("You are from the USA")
        break
    else:
        print("Enter the country Germany or USA")

recentLoc = ""

while recentLoc != us and recentLoc != ger:
    recentLoc = input("What is your location right now?")

    if recentLoc == ger:
        print("You are in Germany right now")
        break
    elif recentLoc == us:
        print("You are in the USA right now")
        break
    else:
        print("Please enter the country Germany or the USA")


while 1:
    try:
        temp = input("What is the temperature outdoor tomorrow?")
        temp = float(temp)
        break
    except ValueError:
        print("That's not a number!")

Hope this helps you a bit...

DerMolly
  • 464
  • 5
  • 17