← 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]
Voters
CodingElf66
KrisLee4
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:

```.css-o0gntx{position:absolute;left:-9999px;}.css-1v1ju80{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:relative;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}x = 20
z = 36

for y in range(1,101):
if x + 2y == z:
print("There is a solution, y is " + str(y))

break

else:
print("There is no such solution for y")```

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

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:

`.css-o0gntx{position:absolute;left:-9999px;}.css-1v1ju80{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:relative;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}x+2*y=z`

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