0
Please help with this code
iv written a simple math code A = input ( ) B = input ( ) C = input ( ) D = input ( ) E = input ( ) print ((int(A) + int(B)+ int (C) + int (D) + int (E) / 5)) works with single numbers eg 1 2 3 4 5 but if I put 1.234 1.345 and so on it does not work any help
6 odpowiedzi
+ 8
Try using float() instead of int()
+ 5
You can define a function which cast for you a number as int else float ( that's the name in Python, there's no 'double' type ;) ) only if needed ( this can be helpful for dealing with numerical floating representation which could introduce some error of rounding ):
def number_cast(nb_str):
return float(nb_str) if '.' in nb_str else int(nb_str)
# and cast input directly, to keep print expression readable
A = number_cast(input ( ))
B = number_cast(input ( ))
C = number_cast(input ( ))
D = number_cast(input ( ))
E = number_cast(input ( ))
print (A + B + C + D + E / 5)
Accessory, your expression is equivalent to A+B+C+D+(E/5) and not to (A+B+C+D+E)/5... for the second case, you need to put explicits parenthesis ^^
+ 3
I don't know python3 but I do know programming basics and if I'm correct, your problem is that you're trying to input numbers of data type "double" into data type "integer" (int) variables which is not possible.
int (integer) - is whole numbers 1, 2, 3, etc
double (double) - is numbers with decimal values like 1.5, 2.5, etc
So try this:
print ((double(A) + double(B)+ double (C) + double (D) + double (E) / 5))
-for double values
+ 2
Replace INT with FLOAT
+ 1
Use either "Double" or "Float" instead of "Integer"
0
thank you will try them out