Why do this code always returns that the value of nx is 0.0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why do this code always returns that the value of nx is 0.0?

n1=float(input("n1")) n2=float(input("n2")) r=n2-n1 nx=str(input("Nx: ")) nx=float() def aritmetica_n(nx): nx=nx*r return nx print("value from n "+str(nx) The function should multiply the value given (nx) and then print the result but it always print 0.0

21st Dec 2018, 4:21 PM
albr3cht
albr3cht - avatar
7 Answers
+ 4
Nope. It will just create an empty float object, aka 0.0. You could do nx = float(nx). But Taste 's approach is more elegant 👆
21st Dec 2018, 5:14 PM
Anna
Anna - avatar
+ 3
Line 5 Shouldnt it be nx=float(nx) ?
21st Dec 2018, 4:29 PM
Taste
Taste - avatar
+ 2
And why making a function without calling it?
21st Dec 2018, 4:32 PM
Paul
Paul - avatar
+ 2
First you define nx=str(input("Nx: ")). Then you redefine it as nx=float(). So whatever was entered before will be lost and nx will be overridden with 0.0 (the standard value of float()). As already pointed out, the function aritmetica_n(nx) is never called so it won't do anything. Why are the lines nx=float() etc. indented?
21st Dec 2018, 4:53 PM
Anna
Anna - avatar
+ 1
Thanks!!
21st Dec 2018, 8:09 PM
albr3cht
albr3cht - avatar
0
Sorry there is a last line of code: print(aritmetica_n(nx))
21st Dec 2018, 5:04 PM
albr3cht
albr3cht - avatar
0
Won't nx=float() transform nx into float??
21st Dec 2018, 5:05 PM
albr3cht
albr3cht - avatar