M
M
Maximus872022-03-15 10:11:15
Python
Maximus87, 2022-03-15 10:11:15

How to convert the resulting variable into a numeric one?

Good afternoon. Sorry I'm new.
I use a variable in the code (it reads the number of files in the directory), then I need to convert this variable into a numeric one, so that I can use it further for mathematical operations: example:

import os
def fcount(path):
    #Counts the number of files in a directory - коли
    count = 0
    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path, f)):
            count += 1

    return count
a =  r"C:\Python" 
# a переменная которую надо конвертировать
c = float( input('Введите от фасованное количество продукта! ') )
d = a / c
print( str(d))


An error occurs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2022-03-15
@Maximus87

Where exactly is this?

it reads the number of files in a directory

If you expect that
a = r"C:\Python"
magic will work for you, you are mistaken
. But writing the result of fcount execution into a is exactly what you need.

V
Vindicar, 2022-03-15
@Vindicar

The above code should work, in theory. Please provide an example of the input that causes the error and the full text of the error.
Based on the conversion to float, I can only assume that you are entering a number with a decimal point ("2.3") and not a decimal point ("2.3").

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question