Topic: Conversion Functions

In my computer class we have an assignment where we have to make our own conversion functionns. There's one for converting to integers, one for fractions, and one called string_to_float. Our prof gives us tips, and he said we need to

Thumbs up

Re: Conversion Functions

sorry it didnt show the whole thing, here's the rest:

In my computer class we have an assignment where we have to make our own conversion functionns. There's one for converting to integers, one for fractions, and one called string_to_float. Our prof gives us tips, and he said we need to "traverse the string to find a decimal point." I'm not really sure what traverse means, but I think it means just find weather there's a decimal.

I used the find function, but I'm not sure how to use it. For, example, let st = the string the user inputs.
find(st,".")
Can I even say it like that?

Should I assign this as a variable and then use it in an if statement? In the other conversion funtions I used a for loop, so should I try to use that here as well?

Or am I doing this completely wrong? Anyways, thank you in advance for any advice you can give me

Thumbs up

Re: Conversion Functions

i dont know if this is what your looking for but the built-in function float(string or number) converts a string or number to a float point number.

>>> string='3.141592653589793238462643383279502884197'
>>> float(string)
3.141592653589793
>>>

it cuts it off at the quadrillionth or 100 billionth digit
i'll try to find something else.

Thumbs up

Re: Conversion Functions

the built-in function eval(x) does the same thing

>>> string='3.141592653589793238462643383279502884197'
>>> eval(string)
3.141592653589793
>>>

Thumbs up

Re: Conversion Functions

for these built in functions it doesn't  matter whether you assign the string to a variable

>>> float('3.141592653589793238462643383279502884197')
3.141592653589793
>>> eval('3.141592653589793238462643383279502884197')
3.141592653589793

Thumbs up