1

I'm a bit of a Python n00b and I'm having trouble ending my python program - its a Pythagoras calculator. I'm unsure why, but it doesn't seem to want to end when I enter 'n'! My source code is below:

from time import sleep
import math
while True:
    print("Welcome to the pythagoras calculator!")
    sleep(1)
    print("This calculator will only calculate side lengths!")
    sleep(1)
    print("Is the side you want:\n 1: The hypotenuse \n Or \n 2: Not the hypotenuse \n Please enter your response")
    sleep(0.5)
    sideType = input()
    try:
        sideType = int(sideType)
        pass
    except:
        print("Error - invalid input \n Please re-enter your data")
        continue
    sleep(0.5)
    print("Thank you.")
    if sideType == 1:
          print("Please enter the length of one side")
          sideOne = input("")
          try:
              sideOne = float(sideOne)
              pass
          except:
              print("Error - invalid input \n Please re-enter your data")
              continue
          sleep(0.5)
          print("Please enter the length of the other side")
          sideTwo = input()
          try:
              sideTwo = float(sideTwo)
              pass
          except:
              print("Error - invalid input \n Please re-enter your data")
              continue
          sleep(0.5)
          print("Thank you.")
          sleep(0.5)
          answerForHypLen = math.sqrt(math.pow(sideOne,2)+math.pow(sideTwo,2))
          print("Calculations complete: \n Your answer is: " + str(answerForHypLen))
    elif sideType == 2:
          print("Please enter the length of the hypotenuse")
          sideHyp = input("")
          try:
              sideHyp = float(sideHyp)
              pass
          except:
              print("Error - invalid input \n Please re-enter your data")
              continue
          sleep(0.5)
          print("Please enter the length of the other side")
          sideNotHyp = input()
          try:
              sideNotHyp = float(sideNotHyp)
              pass
          except:
              print("Error - invalid input \n Please re-enter your data")
              continue
          sleep(0.5)
          print("Thank you.")
          sleep(0.5)
          answerForNotHypLen = math.sqrt(math.pow(sideHyp,2)-math.pow(sideNotHyp,2))
          print("Calculations complete: \n Your answer is: " + str(answerForNotHypLen))
    sleep(0.5)
    print("Would you like to continue?")
    contYN = input()
    if contYN == 'Y'or 'y':
        continue
    elif contYN != 'Y' or 'y':
        False
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • tl;dr: `if contYN == 'Y'or 'y'` is always `True` – vaultah Mar 30 '15 at 17:13
  • Just to clarify. Your using python3. The `contYN = input()` should be replaced by `contYN = raw_input()` in python2.v if you don't want to receive a run-time NameError. – Jerry Ajay Mar 30 '15 at 17:45

1 Answers1

3

You need to change these lines:

if contYN == 'Y'or 'y':
    continue
elif contYN != 'Y' or 'y':
    False

To:

if contYN.upper() == 'Y':
    continue
else:
    break
MrAlexBailey
  • 5,219
  • 19
  • 30