Ask coding questions

← Back to all posts
programming help (python, very urgent!!!)
KrisLee4

hello, all those out there, I need some help with this question that I need to solve right now.

this is the problem:
x +2y = z; x = 20, z = 36. Write a program to find the value of y, by replacing (looping) integer values from 1 to 100 for y. When the left side equals the right side of the equation, print the value of Z.

can anyone give me the answer or part of the answer to this?
thx.

Answered by CodingElf66 [earned 5 cycles]
View Answer
Voters
CodingElf66
KrisLee4
Comments
hotnewtop
CodingElf66

I'll try to explain this step by step. So, when you declare the expression that:

x + 2y = z
x = 20
z = 36

We can substitute the variables in to the equation. So, our equation would now be:

20 + 2y = 36

You want to use a loop (for loop) to find the value of y. So, you can do the code below:

First, we use the information you gave to substitute into the equation. Then, we use a for loop to find every value of y from 1 to 100 (101-1, that's how for loops work). We then use a if statement inside to see if the equation is true or not. The else after the if statement is to see if the for loop wasn't breaked. We then print what we solved!

Whippingdot

wait, after the print in the if statement you should add a break so that the else does not get triggered. @CodingElf66

CodingElf66

@Whippingdot Sorry forgot it, I forgot it. Lol

CodingElf66

@Whippingdot There you go, I just updated it.

Whippingdot
CodingElf66

Do you get it? If you need any further help, always feel free to ask!

CodingElf66

Ok, I'll help you!

KrisLee4

@CodingElf66
thank you so much!

CodingElf66

@KrisLee4 No problem!

CodingElf66

@KrisLee4 I'll be glad to help if you need it, always feel welcome!

KrisLee4

hi @CodingElf66,
i just did as you advised but got this:
File "main.py", line 3
x+2y = z
^
SyntaxError: invalid syntax

what should i do?
thx

CodingElf66

@KrisLee4 Oh yeah, Python doesn't allow you to do that. Sorry, I forgot.

It is:

The * is multiply in Python, if we do that we get an SyntaxError.