-4

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

Rahul Chandrabhan
  • 2,443
  • 5
  • 20
  • 31

3 Answers3

2

When you use input() the input comes as a string, and in your while loop you set your condition to be equal to 1 (as an integer).

A solution to this would be:

varname = int(input("")) #this way it converts your input into an integer
Azeem
  • 7,094
  • 4
  • 19
  • 32
R S
  • 33
  • 6
0

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"))
0

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.

Mushif Ali Nawaz
  • 3,066
  • 3
  • 13
  • 26