3

I am attempting to learn how to program. I really do want to learn how to program; I love the building and design aspect of it. However, in Java and Python, I have tried and failed with programs as they pertain to objects, classes, methods.. I am trying to develop some code for a program, but im stumped. I know this is a simple error. However I am lost! I am hoping someone can guide me to a working program, but also help me learn (criticism is not only expected, but APPRECIATED).

class Converter:

    def cTOf(self, numFrom):
        numFrom = self.numFrom
        numTo = (self.numFrom * (9/5)) + 32
        print (str(numTo) + ' degrees Farenheit')
        return numTo

    def fTOc(self, numFrom):
        numFrom = self.numFrom
        numTo = ((numFrom - 32) * (5/9))
        return numTo

convert = Converter()

numFrom = (float(input('Enter a number to convert..                '))) 
unitFrom = input('What unit would you like to convert from.. ')
unitTo = input('What unit would you like to convert to..    ')

if unitFrom == ('celcius'):
    convert.cTOf(numFrom)
    print(numTo)
    input('Please hit enter..')


if unitFrom == ('farenheit'):
    convert.fTOc(numFrom)
    print(numTo)
    input('Please hit enter..')
1
  • And if you're going to use classes with this, might as well make those methods static Commented Oct 14, 2012 at 3:41

3 Answers 3

4

Classes and objects are tools to accomplish a task -- they allow you to encapsulate data or state with a set of methods. However, your data is just a number. There is no need to encapsulate the integer, so there is no need to create a class.

In other words, don't create a class because you think you should, create a class because it makes your code simpler.

import sys

def f_to_c(x):
    return (x - 32) * (5/9)

def c_to_f(x):
    return x * (9/5) + 32

num_from = float(input('Enter a number to convert: '))
unit_from = input('What units would you like to convert from? ')
unit_to = input('What units would you like to convert to? ')

if (unit_from, unit_to) == ('fahrenheit', 'celsius'):
    num_to = f_to_c(num_from)
elif (unit_from, unit_to) == ('celsius', 'fahrenheit'):
    num_to = c_to_f(num_from)
else:
    print('unsupported units')
    sys.exit(1)

print('{} degrees {} is {} degrees {}'
      .format(num_from, unit_from, num_to, unit_to))
Enter a number to convert: 40
What units would you like to convert from? celsius
What units would you like to convert to? fahrenheit
40.0 degrees celsius is 104.0 degrees fahrenheit

The convert object and Converter class don't serve any purpose, so the code is simpler and easier to read without them.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought it would make it simpler in the end. However, I was wrong apparently. Smaller bites next time! Thanks for the helpful answer
1

1.It should be

def fTOc(self, numFrom):
    self.numFrom = numFrom

cTOf method has the same problem.

2.Variable numTo not defined

numTo = convert.cTOf(numFrom)
print (numTo)

2 Comments

Since numFrom isn't used anywhere else in the class, why make it a member variable at all?
@NullUserException You are right, I just debug the code to make it work.
0

You almost got it right.

There is no self.numFrom because it is your parameter. Remove the lines numFrom =self.numFrom and you will be fine.

1 Comment

cTOf used self.numFrom later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.