Skip to main content
added 230 characters in body
Source Link
Carcigenicate
  • 16.6k
  • 3
  • 37
  • 82

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok doing so doesn't create problems. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything. What if you wanted to send the raw data over the internet or save it to a file?

  • You're putting an empty return at the end. This is redundant though and unnecessary. None is automatically returned at the end anyways if no return is met, which is equivalent to what you're doing.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        is_celsius = selection == 'c' 
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32

def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32

def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this doing so doesn't create problems. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything. What if you wanted to send the raw data over the internet or save it to a file?

  • You're putting an empty return at the end. This is redundant though and unnecessary. None is automatically returned at the end anyways if no return is met, which is equivalent to what you're doing.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        # Save if we're converting to Celsius or not
        is_celsius = selection == 'c' 
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32

def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

added 16 characters in body
Source Link
Carcigenicate
  • 16.6k
  • 3
  • 37
  • 82

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32
 

def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

The is_celcius checks could be condensed using tuples too to have less duplication. I'll show how that can be done when I get on my computer. I don't want to lose the draft on my phone.

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32
 

def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

The is_celcius checks could be condensed using tuples too to have less duplication. I'll show how that can be done when I get on my computer. I don't want to lose the draft on my phone.

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32

def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

added 74 characters in body
Source Link
Carcigenicate
  • 16.6k
  • 3
  • 37
  • 82

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32


def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

The is_celcius checks could be condensed using tuples too to have less duplication. I'll show how that can be done when I get on my computer. I don't want to lose the draft on my phone.

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32


def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. Do whatever you feel is more readable.

The is_celcius checks could be condensed using tuples too. I'll show how that can be done when I get on my computer. I don't want to lose the draft on my phone.

Your conversion functions have two notable things:

  • You're printing the result directly. Don't do this. For toy programs like this, this is ok. In the real world though, you don't just print data to the screen, you use data. With how you have it now, the caller can't actually use the converted value for anything.

  • You're putting an empty return at the end. This is redundant though and unnecessary.

I would have the functions return the converted value, and print it at the call site:

def value_input(selection):
    value = input('''\nPlease enter the temperature you
want to convert: ''')
    try:
        value = float(value)

    except ValueError:
        print('That is not a number!\n')

    else:
        is_celsius =  selection == 'c' # Save if we're converting to Celsius or not
        
        new_value = convert_c2f(value) if is_celsius else convert_f2c(value)
        unit_str = "C" if is_celsius else "F"
        
        print(f'The answer is: {new_value}°{unit_str}')


def convert_c2f(value):
    return (value * (9 / 5)) + 32


def convert_f2c(value):
    return (value - 32) * (5 / 9)

I decided to reduce the printing down to a single call to print and just decide what data will be printed ahead of time. This is personal style though. I get an odd thrill out of reducing duplication. Do whatever you feel is more readable.

The is_celcius checks could be condensed using tuples too to have less duplication. I'll show how that can be done when I get on my computer. I don't want to lose the draft on my phone.

added 195 characters in body
Source Link
Carcigenicate
  • 16.6k
  • 3
  • 37
  • 82
Loading
Source Link
Carcigenicate
  • 16.6k
  • 3
  • 37
  • 82
Loading