0

The function Celsius2Fahrenheit will convert Celsius to Fahrenheit to be used later, and the range is supposed to go up each time by .5, and stop at 101, but you cant use a float value in range, (I am a beginner at python), can someone please help.

def Celsius2Fahrenheit(c):
""" This will Convert c Celsius to its Fahrenheit equivalent"""
return c * 9 / 5 + 32
for x in range(0,101,.5):
# This will print the values using new style formatting
    e = Celsius2Fahrenheit(x)
    if (e > 0):
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
    else:
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

4 Answers4

1

Here are two possibilities:

1) Multiply your range by 10 so that the values become integers you can use with range(). Then you would divide the index variable by 10 to get back the float values you are after, like this:

for ix in range(0, 1010, 5):
    x = ix / 10
    <...rest of your code...>

2) You can use arange() method from numpy instead:

import numpy

for x in numpy.arange(0, 5.5, 0.5):
    e = Celsius2Fahrenheit(x)
    <...rest of your code...>

See reference for more details about the arange()

Emrah Diril
  • 1,687
  • 1
  • 19
  • 27
0

Because you can increment range like you that.

Try this,

import numpy as np

def Celsius2Fahrenheit(c):
#This will Convert c Celsius to its Fahrenheit equivalent"""
  return c * 9 / 5 + 32

Use numpy arange,

for x in np.arange(0, 101, 0.5):
# This will print the values using new style formatting
    e = Celsius2Fahrenheit(x)
    if (e > 0):
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
    else:
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
merit_2
  • 461
  • 5
  • 16
0

There are two basic ways you could do this, by using a while loop and modifying a value yourself, or changing the bounds for the range function.

The first method is likely preferred as it will be more accurate and easy to code.

def Celsius2Fahrenheit(c):
    """ This will Convert c Celsius to its Fahrenheit equivalent"""
    return c * 9 / 5 + 32

"""While Loop Method"""
x = 0
while x <= 100:
    e = Celsius2Fahrenheit(x)
    if (e > 0):
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
    else:
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
    x += 0.5

print()

"""Higher Range"""
import math
step = 0.5
for x in range(0,math.ceil(100.5/step)):
    e = Celsius2Fahrenheit(x*step)
    if (e > 0):
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
    else:
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))

Please be aware that for the second method, the best way to go about it is to add the step value to the upper bound of range. For example:

for x in range(0,math.ceil((100+step)/step)):

This will go upto the non-step value (in this case 100) inclusively.

Misha Melnyk
  • 409
  • 2
  • 5
0

try this

def Celsius2Fahrenheit(c):
    return c * 9 / 5 + 32


for x in map(lambda x: x/10.0, range(0, 1005, 5)):
# This will print the values using new style formatting
    e = Celsius2Fahrenheit(x)
    if (e > 0):
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))
    else:
        print(" {:3.1f} (C) | {:6.2f} (F) ".format(x,e))

This will create list of values from 0 to 100 in steps of .5