return doesn't return | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

return doesn't return

i'm a new in programing at all , also my english is bad (sorry) :) this script for calculate 'Ohm law volt , amp and resistance' its my first time to use classes : https://code.sololearn.com/c1re0R7M4dgp/#py in line 31 , i want to return error message , but after the program start and reach to this point in line 73 , its doesn't return the message , but the other case return correctly. how i can fix it

30th Oct 2017, 11:36 AM
khaled Fathi
khaled Fathi - avatar
6 Answers
+ 2
It's not actually the return() that is at fault. ur code need any two inputs out of i, v &r to make sense and you have ensure that atleast two are entered using the exception handling code (eye++). But, that system doesn't prevent the user from entering zero(0) as value for one or both of the required values. Now, if you look closely, ur error statement should be printed if any one or both required variables is/are zero. But since u used the code:: if v and i to determine if u needed to calculate r, the code in the if block is executed only if both v and i are non-zero... Hence, the class functions are never called with erroneous values, i.e v or i as zero(0). So is the case for remaining two combinations, i.e (i and r) and (r and v)... Instead of checking if(i and r), check if not v.. or something else of that sort
30th Oct 2017, 3:03 PM
Anil
Anil - avatar
+ 1
nooooooo.... now u seem to have repeated the error over. undo the changes to ur class except the 'pf' addition part. all u needed to change was the code outside the class, the one getting the input, those if statements needed change.
30th Oct 2017, 3:29 PM
Anil
Anil - avatar
+ 1
thanx for ur intresting , i did what u said , i'll practice more thanx again
30th Oct 2017, 3:36 PM
khaled Fathi
khaled Fathi - avatar
+ 1
TRY THE FOLLOWING BARE-BONES CODE I HAVE CREATED. Do not forget to incorporate your exception handling mechanism into the code (your except handling was great and intuitive...) In this code, the return is always called because it checks the conditionality based on the value of the uknown variable, in this case: 'v' GOOD LUCK. class ohm: def __init__(self,r=0, v=0, i=0,pf=0): self.r, self.v, self.i, self.pf = r,v,i,pf def get_volt(self): if self.r>0 and self.i>0: return(self.r*self.i) else: return("Error: both Current & Resistance needs to be non-zero") print("\n\nPlease enter the values of Resistance(r), Voltage(v) and Current(i) in that order.") r = int(input("Resitance :")) v = int(input("Votage :")) i = int(input("Current :")) o = ohm(r,v,i) if not(v): v = o.get_volt() print("Votage calculated is: "+str(v))
30th Oct 2017, 3:53 PM
Anil
Anil - avatar
0
now i understod it , and i changed the code in class , can u check it and tell me if its better now or not , https://code.sololearn.com/cYQujrIp62iF/#py sorry about my bad english -- thanx :)
30th Oct 2017, 3:21 PM
khaled Fathi
khaled Fathi - avatar
- 1
I am cry
23rd Apr 2020, 1:55 PM
Aleksandra Vinnikova
Aleksandra Vinnikova - avatar