Home > Enterprise >  How to end my code while i am using function?
How to end my code while i am using function?

Time:11-05

need your help guys quickly, so i have to enter a number that does calculations , but then it asks if i want to enter another number. if i press y or Y ir starts again , if i press any other button it ends, but it seems that my code dose not end . pls help

    import math
import sys
def start():
  t = input()
  x = int(t)
  try:
    a = (3 * math.pow(x, 4) - 12 * math.pow(x, 2) - 1)/((x   2)*(x - 2))  # aprekina a(x)
    b = math.log(3 * x   5 * x)  # aprekina b(x)

    if a * b < 50:  # parbauda nosacijumu
      f1 = 1 / math.pow(a, 2) - math.pow(a, 2) * b - 11  # ja nosacijums y tad aprekina f1
      print(round(f1, 2))  # izvada f1
    else:
      f2 = 16 * a * b - 2 * math.sqrt(2)  # ja nosacijums n tad aprekina f2
      print(round(f2, 2))  # izvada f2
  except ValueError:
    print("error")
  except ZeroDivisionError:
    print("error")

  c = input("repeat?")
  if c == "y" or "Y":
    start()
  else:
    sys.exit  
start()

CodePudding user response:

if c == "y" or "Y": will always return true because any String that isn't an empty string ("") will be True so "Y" will always be True

You need to write:

if (c == "y" or c == "Y"):

This will properly check if c equals lowercase or uppercase y.

You can also write:

if (c.lower() == "y"):

CodePudding user response:

if c == "y" or "Y"

this condition is always True and your code never ends!

because "Y" is always True!!!

Try this:

if c == "y" or c == "Y"
  • Related