+ 2

Decide zero, or continue (need help)

Hi. I'm new to programming so I apologise if this question is stupid but I've spend about a week on this and I can't figure it out and I feel like my head is going to explode from so much information. But now to the point. I would need the program to decide if the number is zero, and if it is to print a message, and of it's not, then remember the value and keep working with it. Example: x1=int(input()) x2=int(input()) x3=int(input()) c=x1+x2-x3 if c==0: print ("zero") else: a=(x1+x3) /c <- This is basically where the program would decide that c is not zero and calculate the given example, and then save it and work with it later. number=a And then I would need to continue with that number like d=number*x1*x2 I need it to work somehow like that. I've tried all sorts of things but I just don't know anymore. This is probably incredibly stupid what I wrote. But hopefully it shows what I want to do. Thank you all for your help.

22nd Oct 2021, 9:51 AM
Jeanie Snow
Jeanie Snow - avatar
40 Antworten
+ 5
If I understand you correctly you want to write a program that takes in the coordinates of two pairs of points and calculate where the line through the first pair of points and the line through the second pair of points intersect. You already have the formula for calculating those points. If the lines do not intersect (i.e. if they are parallel) the program should be able to handle that, too. The lines are parallel iff the denominator in the formula for the coordinates is zero, so you can deal with this using a try-except block. Here's a code that does that. Instead of taking input, I've put in five sample lines to quickly play around with. Two of those are parallel to allow testing for that case. Is this what you're looking for? https://code.sololearn.com/cDdG3iK6SUVp
22nd Oct 2021, 4:51 PM
Simon Sauter
Simon Sauter - avatar
+ 3
In a 2D system they will always be on the same plane
22nd Oct 2021, 4:23 PM
Lisa
Lisa - avatar
+ 3
Use else statement instead of finally. Or just If c = = 0 print ("zero) else: do all other stuff, not just a =
22nd Oct 2021, 9:56 PM
Константин Иванов
Константин Иванов - avatar
+ 2
You could use a while loop: while c is not 0, go on, if c is 0 then break
22nd Oct 2021, 9:53 AM
Lisa
Lisa - avatar
+ 2
Go to code section, click the green button "+ New Code", select the programming language, insert your code and don't forget to save it. The you can click the little "+" i t the thread to attach your code
22nd Oct 2021, 11:18 AM
Lisa
Lisa - avatar
+ 2
In the app: - go to "Codes" at the center bottom - tap the green button to create a new code - select the programming language you want - write your code - save your code and make it public - come back here - click on the plus inside a circle in the text field - choose "Add Code" - click on the pull-down menu in the top left - switch to "My Code Bits" - choose the code(s) you want to link to - post
22nd Oct 2021, 11:23 AM
Simon Sauter
Simon Sauter - avatar
+ 2
Yes. Thank you. This is what I wanted to do.
22nd Oct 2021, 5:25 PM
Jeanie Snow
Jeanie Snow - avatar
+ 2
"Thank you! But how I write it in python? Like what I write instead of "do all other stuff, not just a="?" Like this? https://code.sololearn.com/cn2Urf11a4If/?ref=app https://code.sololearn.com/cibZHqiJHhr2/?ref=app
23rd Oct 2021, 8:08 AM
Константин Иванов
Константин Иванов - avatar
+ 1
Thank you. It's working fine when it's 0. But when c is not 0, it's continue cycling. Because the number doesn't change. Is there some way to print zero and then break?
22nd Oct 2021, 10:22 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
If the number is 0, the program would write "Zero" and the program would end. And if the number is < 0 or > 0, the program would take the number and continue work with it.
22nd Oct 2021, 10:34 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
The maybe something like that: while number != 0: # do some calculations # (adjust number and go on with next iteration)
22nd Oct 2021, 10:36 AM
Lisa
Lisa - avatar
+ 1
Maybe. But won't it cycle too?
22nd Oct 2021, 10:40 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
Maybe a try-except block is what you need: https://code.sololearn.com/cz7Nr4WL2HmG/?ref=app
22nd Oct 2021, 10:50 AM
Lisa
Lisa - avatar
+ 1
It still write that, other two values are not defined. I don't know, how to do code here. To show you my code. But I need to take the c and then work with him with t an u and then print "Yes" or "Not"
22nd Oct 2021, 11:14 AM
Jeanie Snow
Jeanie Snow - avatar
22nd Oct 2021, 11:37 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
The "finally" part will always be executed no matter if the "try" part succeeds or not. But t and u are only defined, when try does not fail. So I think the code that you but in "finally" should rather go into the "try" part
22nd Oct 2021, 11:42 AM
Lisa
Lisa - avatar
+ 1
Ohh, I see. Thank you very much. I'll try it.
22nd Oct 2021, 11:51 AM
Jeanie Snow
Jeanie Snow - avatar
+ 1
a=int(input(”Enter a value :”)) b=int(input(”Enter b value :”)) try: c = a / b except ZeroDivisionError: print(”cannot divide by zero”) except Exception: print(”Exception handled”) print c
22nd Oct 2021, 12:58 PM
sree harsha
sree harsha - avatar
+ 1
i didn't understand one thing that when c not equals O, do you want to input values? can you plz explain the problem step by step
22nd Oct 2021, 1:58 PM
Krishnam Raju M
Krishnam Raju M - avatar