1 Replies - 3392 Views - Last Post: 05 March 2010 - 04:46 AM Rate Topic: -----

#1 Guest_Gretty*


Reputation:

Temp Converter Not Recognising raw_input

Posted 05 March 2010 - 01:37 AM

Hello

I am very new to Python & I have made a Temperature converter. But when I try to use the function raw_input() I get a compile error:

Quote

input = int(raw_input(printMenu())) # Print menu
NameError: global name 'raw_input' is not defined


Also do you think my code needs to be written better for python. I am coming from C++ & writting it in its architecture kindof, ie, using a main function etc. Is that how you usually write in Python, what is best?

# Temperature Converter 5/3/2010

import sys

def printMenu():
    print("""
          *** Temperature Converter ***
          1. Convert from Celsius to Fahrenheight
          2. Convert from Fahrenheight to Celsius
          3. Exit
          """)

def celToFahr(n):
    return( ((n*9)/5)+32 )

def fahrToCel(n):
    return( ((n-32)*5)/9 )


def main():

    input = 0
    
    while(input!=3):

        input = int(raw_input(printMenu())) # Print menu

        if (input == 1):
            num = float(raw_input("Please enter a number to convert: "))
            result = celToFahr(num)
            print(result)
        elif (input == 2):
            num = float(raw_input("Please enter a number to convert: "))
            result = fahrToCel(num)
            print(result)  # print conversion
        if (input == 3):
            break

    return

# start program(Call Main Function)
main()



Is This A Good Question/Topic? 0

Replies To: Temp Converter Not Recognising raw_input

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Temp Converter Not Recognising raw_input

Posted 05 March 2010 - 04:46 AM

To to your print functions, it looks like you are using Python 3.*. In Python 3.0 and above, raw_input() was replaced with just input().

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1