So I am trying to write a simple code that will do the Pythagorean theorem for me after I input A, B and C but the code is skipping my While statements, and I have tried rewriting them as if statements to see if that works and again it will skip it, I need some help Please and Thank you Btw I do realize that in the picture that my while loops are open and have nothing ending them but I did have that in there at one point but I had taken them out when I changed to If statements.My Code I cant seem to understand
3 Answers
As the python documentation points out, the input function returns a string:
input([prompt]) If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.
If you didn't know that and you wanted to debug and figure out, you can do something like print(type(Yes_No))
and you can see that it is a string type, so when you evaluate this expression: while Yes_No == 1
, it returns false.
So the fix in this situation is to change your input line to
Yes_No = int(input("Do you have the hypotenuse? For yes press 1 or for no press 2"))
![](../../users/profiles/4672536.webp)
- 11
- 5
-
Thanks for the clarification that explains a lot, and will probably help with future questions so thank you once again – The Creeper Clan Mar 08 '18 at 14:03
When you're taking input()
from the user, it is returned as a string. Suppose user enters 1
it will be stored as "1" # which is a string
. Now when you compared Yes_No == 1
it returned False
because "1" == 1
is False
.
So you need to parse (convert) it into a number (integer), which can be done by passing string to int()
function. It will return the integer representation of that string. Do the same with all the inputs and your problem will be solved!
Another problem with your code is that you're not updating the value of Yes_No
in any of the while
loop. Which means that it will result in infinite loop, it will keep executing the while loop because once the condition becomes True
it will not become False
because value of Yes_No
is not updated.
![](../../users/profiles/5413565.webp)
- 3,066
- 3
- 13
- 26