Converting Fahrenheit and Celsius in python

Hello, I'm writing a program for a class and I'm stuck.
This is the problem
Write an application that will accept the following inputs: low temperature, high temperature, and type of temperature values that have been entered where F is for Fahrenheit and C is for Celsius. Be sure to use appropriate prompts and instructions for all inputs required. An output table will be created starting at the low temperature and ending with the high temperature with increments of one degree in the first column. In the second column the corresponding equivalent temperature of the other type will be listed. Be sure that the low temperature is indeed lower than the high temperature. Also the user must enter F or C in either upper or lower case and should be continuously queried until a valid value has been entered.
Here is my code so far for it.

print "Please answer the following questions."

F = Fahrenheit
C = Celsius

raw_input("

Please enter a F for fahrenheit or a C for Celsius: ")

int(raw_input("

Please enter a low Temperature: "))

int(raw_input("

Please enter a high Temperature: "))

if (low_temp > high_temp):
print "Sorry, please enter new temperature."

# Figure out if temperature is Fahrenheit or Celsius
if F:
print C
else:
print F

C == ((5/9 * temp) 32))
F == ((4/5 * temp) + 32))

raw_input("

Please hit the enter key to exit.")

Can anyone help point me in the right direction?
Thanks

Comments

  • [b][red]This message was edited by Drost at 2004-10-19 3:19:42[/red][/b][hr]
    : Hello, I'm writing a program for a class and I'm stuck.
    : This is the problem
    : Write an application that will accept the following inputs: low temperature, high temperature, and type of temperature values that have been entered where F is for Fahrenheit and C is for Celsius. Be sure to use appropriate prompts and instructions for all inputs required. An output table will be created starting at the low temperature and ending with the high temperature with increments of one degree in the first column. In the second column the corresponding equivalent temperature of the other type will be listed. Be sure that the low temperature is indeed lower than the high temperature. Also the user must enter F or C in either upper or lower case and should be continuously queried until a valid value has been entered.
    : Here is my code so far for it.
    :
    : print "Please answer the following questions."
    :
    : F = Fahrenheit
    : C = Celsius
    :
    : raw_input("

    Please enter a F for fahrenheit or a C for Celsius: ")
    :
    : int(raw_input("

    Please enter a low Temperature: "))
    :
    : int(raw_input("

    Please enter a high Temperature: "))
    :
    : if (low_temp > high_temp):
    : print "Sorry, please enter new temperature."
    :
    : # Figure out if temperature is Fahrenheit or Celsius
    : if F:
    : print C
    : else:
    : print F
    :
    : C == ((5/9 * temp) – 32))
    : F == ((4/5 * temp) + 32))
    :
    : raw_input("

    Please hit the enter key to exit.")
    :
    : Can anyone help point me in the right direction?
    : Thanks
    :

    As far as I know the formulas are:
    Celsius = (Temp_in_Fahrenheit - 32) / (9.0/5.0)
    Fahrenheit = (Temp_in_Celsius * (9.0/5.0)) + 32

    Also look after what your teacher told you about variables and value assignments... and about conditions and comparisons... and about iterations... and about calling subroutines. Those seem to bee problematic issues in your approach.

    Drost

  • : Hello, I'm writing a program for a class and I'm stuck.

    Yes, you have some serious problems with your code.

    : This is the problem
    : Write an application that will accept the following inputs: low temperature, high temperature, and type of temperature values that have been entered where F is for Fahrenheit and C is for Celsius. Be sure to use appropriate prompts and instructions for all inputs required. An output table will be created starting at the low temperature and ending with the high temperature with increments of one degree in the first column. In the second column the corresponding equivalent temperature of the other type will be listed. Be sure that the low temperature is indeed lower than the high temperature. Also the user must enter F or C in either upper or lower case and should be continuously queried until a valid value has been entered.
    : Here is my code so far for it.
    :
    : print "Please answer the following questions."
    :
    : F = Fahrenheit
    : C = Celsius

    What is this? Where are Fahrenheit and Celsius defined?

    : raw_input("

    Please enter a F for fahrenheit or a C for Celsius: ")
    : int(raw_input("

    Please enter a low Temperature: "))
    : int(raw_input("

    Please enter a high Temperature: "))

    For pete's sake, these functions return values but you're not assigning them anywhere.

    : if (low_temp > high_temp):
    : print "Sorry, please enter new temperature."
    :
    : # Figure out if temperature is Fahrenheit or Celsius
    : if F:
    : print C
    : else:
    : print F

    I'm not exactly sure what you're trying to do here by printing C or F.

    : C == ((5/9 * temp) 32))
    : F == ((4/5 * temp) + 32))
    :
    : raw_input("

    Please hit the enter key to exit.")

    This last line is good, except you didn't include any looping anywhere to catch bad input and try again.

    : Can anyone help point me in the right direction?

    Here's a simple script I just hacked out. It should give you a place to start. Note that I left some parts out so you'd have to work on it yourself a bit. The actual conversions are made up, it only handles integer values, and the output table is pretty lame.

    [code]
    def FtoC(temp):
    "converts a fahrenheit temperature to celcius"
    return temp / 10

    def CtoF(temp):
    "converts a celcius temperature to fahrenheit"
    return temp * 10

    def prompt_for_unit():
    unit = ''
    while True:
    unit = raw_input("Enter the units (C or F):")
    if unit in ('C', 'F'):
    break
    else:
    print 'Invalid entry, please try again.'
    return unit

    def prompt_for_start():
    start = ''
    while True:
    start = raw_input("Enter the starting temperature:")
    try:
    start = int(start)
    except ValueError:
    print 'Invalid entry, please try again.'
    else:
    break
    return start

    def prompt_for_end():
    end = ''
    while True:
    end = raw_input("Enter the ending temperature:")
    try:
    end = int(end)
    except ValueError:
    print 'Invalid entry, please try again.'
    else:
    break
    return end

    def prompt_for_range():
    start, end = '', ''
    while True:
    start = prompt_for_start()
    end = prompt_for_end()
    if start < end:
    break
    else:
    print 'Starting temperature must be lower than ending temperature!'
    return start, end

    def main():
    start, end = prompt_for_range()
    unit = prompt_for_unit()
    convert = None
    other = ''
    if unit == 'C':
    convert = CtoF
    other = 'F'
    else:
    convert = FtoC
    other = 'C'
    temp = start
    while temp <= end:
    print ' %d%s %d%s' % (temp, unit, convert(temp), other)
    temp += 1

    if __name__ == '__main__': main()
    [/code]


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • good question to ask and I am glad you did, it was a big help.

    can anyone tell me what I am doing wrong with this?

    def main():
    yyBurger = 0.99
    greaseYFries = 0.79
    sodaYum = 1.09
    tax = 0.06
    total = calc_total (price, tax)
    yes = question()
    #option for how many burgers the user wants
    def yumyumBurger():
    Print ('how many yyburgers would you like?')
    Print #print a blank line
    total_yumyumBurger = input ('enter number of yyBurger for today:')
    return yumyumBurger

    #option for how many greaseYFries the user wants
    def greaseYumfries():
    Print ('How many greaseYFries do you want?')
    Print #print a blank line
    total_greaseYFries = input('enter number of greaseYFries for today:')
    return greaseYFries

    #option for how many soda yum the the user wants
    def sodaYum():
    Print ('How many sodaYums would you like?')
    Print #print a blank line
    total_sodaYum = input ('enter number of sodaYum for today:')
    return sodaYum

    #This will calculate the total bill for the amount of burgers, fries, and sodas
    #that the customer wants
    def calc_total (yyBurgers, greaseYFries, sodaYum, tax):
    total = yyBurger + greaseYFries + sodaYum + tax
    Print ('total')
    return total

    #calls main
    main()
  • Hi,
    Thanks for this useful information. I must appreciate the tools provided by you. Please keep updating me in this regard.

    CSK



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories