u/ice_or_flames

Is this a good way to build a math program?

degree = int(input("Of what degree is your function?[0-51]: "))
degree_value = degree
function = ""
constant = 97
while degree >= 0:
  if degree > 1:    
    function = function + "" + chr(constant) + "(x^" + str(degree) + ") + "
  elif degree == 1:  
    function = function + "" + chr(constant) + "(x) + "      
  elif degree == 0:
    function = function + "" + chr(constant)
  degree = degree - 1
  constant = constant + 1
  if constant == 123:
    constant = 65
print(function)


constant = 97
constants = []
degree = degree_value
while degree >= 0:
   new_constant = (float(input(chr(constant) + " = ")))
   constants.append(new_constant)
   degree = degree - 1
   constant = constant + 1
   if constant == 123:
     constant = 65
number_of_terms = len(constants)


function = ""
location = 0
degree = degree_value
while number_of_terms > 0:
  if constants[location] == 0 and degree == 0:
    function = function + "0"      
  elif constants[location] == 0:
    function = function
  elif constants[location] != 1:
    if degree > 1:
      function = (function + str(constants[location]) + "x^"
      + str(degree) + " + ")
    elif degree == 1:
      function = function + str(constants[location]) + "x + "
    elif degree == 0:
      function = function + str(constants[location])
  elif constants[location] == 1:
    if degree > 1:
        function = function + "x^" + str(degree) + " + "
    elif degree == 1:
        function = function + "x + "
    elif degree == 0:
        function = function + str(constants[location])
  number_of_terms = number_of_terms - 1
  location = location + 1
  degree = degree - 1
print("f(x) = " + function)


derivative = ""
location = 0
degree = degree_value
while degree >= 1:
  if (constants[location] == 0 and degree == 1):
     derivative = derivative + "0"
  elif (constants[location] * degree) != 1 and (constants[location] * degree) != -1:
    if degree > 2:    
      derivative = derivative + str(constants[location] * degree) + "x^" + str(degree - 1) + " + "
    elif degree == 2:
      derivative = derivative + str(constants[location] * degree) + "x + "
    elif degree == 1:
      derivative = derivative + str(constants[location])
  elif (constants[location] * degree) == 1 or (constants[location] * degree) == -1:    
    if degree >= 3:    
      derivative = derivative + "x^" + str(degree - 1) + " + "
    elif degree == 2:
      if constants[location] > 0:
        derivative = derivative + "x + "
      else:
        derivative = derivative + "-x + "
    elif degree == 1:
      if constants[location] > 0:
        derivative = derivative + "1"
      else:
        derivative = derivative + "-1"
  degree = degree - 1
  location = location + 1
print("f'(x) = " + derivative + "\n")
if derivative == "0":
  print("f'(x) cannot equal zero.")    
  quit()
 

print("Newton-Rhapson method:")
finished = False
while not finished:
  x = float(input("Initial guess = "))
  iterations = int(input("How many iterations?: "))
  function = ""
  derivative = ""
  x_n = 1
  number_of_constants = len(constants) - 1
  location = 0
  while number_of_constants >= 0:
    if number_of_constants > 0:
        function = function + str(constants[location]) + " * x ** " + str(number_of_constants) + " + "
        if number_of_constants > 1:
          derivative = derivative + str(constants[location] * number_of_constants) + " * x ** " + str(number_of_constants - 1) + " + "
    if number_of_constants == 1:
        derivative = derivative + str(constants[location])
    if number_of_constants == 0:
        function = function + str(constants[location])
    number_of_constants = number_of_constants - 1
    if number_of_constants >= 0:
        location = location + 1
  print("x0 = " + str(x))
  while x_n <= iterations:
    try:
      x = x - (eval(function))/eval((derivative))
      print("x" + str(x_n) + " = " + str(x))
      x_n = x_n + 1
    except:
      print("f'(x0) cannot equal zero.")
      quit()
  answer = input("Do you want to do another approximation?[y/n]: ")
  if answer == "n":
    finished = True



reddit.com
u/ice_or_flames — 2 days ago

Is this a good way to build a math program?

degree = int(input("Of what degree is your function?[0-51]: "))
degree_value = degree
function = ""
constant = 97
while degree >= 0:
  if degree > 1:    
    function = function + "" + chr(constant) + "(x^" + str(degree) + ") + "
  elif degree == 1:  
    function = function + "" + chr(constant) + "(x) + "      
  elif degree == 0:
    function = function + "" + chr(constant)
  degree = degree - 1
  constant = constant + 1
  if constant == 123:
    constant = 65
print(function)


constant = 97
constants = []
degree = degree_value
while degree >= 0:
   new_constant = (float(input(chr(constant) + " = ")))
   constants.append(new_constant)
   degree = degree - 1
   constant = constant + 1
   if constant == 123:
     constant = 65
number_of_terms = len(constants)


function = ""
location = 0
degree = degree_value
while number_of_terms > 0:
  if constants[location] == 0 and degree == 0:
    function = function + "0"      
  elif constants[location] == 0:
    function = function
  elif constants[location] != 1:
    if degree > 1:
      function = (function + str(constants[location]) + "x^"
      + str(degree) + " + ")
    elif degree == 1:
      function = function + str(constants[location]) + "x + "
    elif degree == 0:
      function = function + str(constants[location])
  elif constants[location] == 1:
    if degree > 1:
        function = function + "x^" + str(degree) + " + "
    elif degree == 1:
        function = function + "x + "
    elif degree == 0:
        function = function + str(constants[location])
  number_of_terms = number_of_terms - 1
  location = location + 1
  degree = degree - 1
print("f(x) = " + function)


derivative = ""
location = 0
degree = degree_value
while degree >= 1:
  if (constants[location] == 0 and degree == 1):
     derivative = derivative + "0"
  elif (constants[location] * degree) != 1 and (constants[location] * degree) != -1:
    if degree > 2:    
      derivative = derivative + str(constants[location] * degree) + "x^" + str(degree - 1) + " + "
    elif degree == 2:
      derivative = derivative + str(constants[location] * degree) + "x + "
    elif degree == 1:
      derivative = derivative + str(constants[location])
  elif (constants[location] * degree) == 1 or (constants[location] * degree) == -1:    
    if degree >= 3:    
      derivative = derivative + "x^" + str(degree - 1) + " + "
    elif degree == 2:
      if constants[location] > 0:
        derivative = derivative + "x + "
      else:
        derivative = derivative + "-x + "
    elif degree == 1:
      if constants[location] > 0:
        derivative = derivative + "1"
      else:
        derivative = derivative + "-1"
  degree = degree - 1
  location = location + 1
print("f'(x) = " + derivative + "\n")
if derivative == "0":
  print("f'(x) cannot equal zero.")    
  quit()
 

print("Newton-Rhapson method:")
finished = False
while not finished:
  x = float(input("Initial guess = "))
  iterations = int(input("How many iterations?: "))
  function = ""
  derivative = ""
  x_n = 1
  number_of_constants = len(constants) - 1
  location = 0
  while number_of_constants >= 0:
    if number_of_constants > 0:
        function = function + str(constants[location]) + " * x ** " + str(number_of_constants) + " + "
        if number_of_constants > 1:
          derivative = derivative + str(constants[location] * number_of_constants) + " * x ** " + str(number_of_constants - 1) + " + "
    if number_of_constants == 1:
        derivative = derivative + str(constants[location])
    if number_of_constants == 0:
        function = function + str(constants[location])
    number_of_constants = number_of_constants - 1
    if number_of_constants >= 0:
        location = location + 1
  print("x0 = " + str(x))
  while x_n <= iterations:
    try:
      x = x - (eval(function))/eval((derivative))
      print("x" + str(x_n) + " = " + str(x))
      x_n = x_n + 1
    except:
      print("f'(x0) cannot equal zero.")
      quit()
  answer = input("Do you want to do another approximation?[y/n]: ")
  if answer == "n":
    finished = True
reddit.com
u/ice_or_flames — 2 days ago

How can I make this code more efficient? (Shorter)

young_flag = False
ya_flag = False
adult_flag = False
old_flag = False
m_flag = False
f_flag = False
x_flag = False
print("Running program...")
print("")
time.sleep(1)
user = ""
while not user.isalpha():
  user = input("What is your name?[only letters]: ")
  if not user.isalpha():
    print("That is not a name.")
    print("")
print("Welcome, " + user)
running_the_game = False
while not running_the_game:
  answer = input("Do you want to enter The Game?[y/n]: ")
  if answer in ("y", "Y", "yes", "Yes", "YES"):
    running_the_game = True
  elif answer in ("n", "N", "no", "NO", "No"):
    print("Your loss, " + user)
    time.sleep(1)
    print("Closing program...")
    time.sleep(1)
    quit()
  else:
    print("Invalid response")
    print("")
print("Running The Game...")
print("")
age_check = False
time.sleep(2)
print("Hello Darling, let us begin.")
time.sleep(1)
age_int = False
while not age_check:
  while not age_int:
    user_age = input("How old are you, my dear?[number]: ")
    try:
      int(user_age)
    except:
      time.sleep(1)
      print("That is not a number, try again, sweetie.")
      time.sleep(1)
    else: age_int = True
  user_age = int(user_age)
  if user_age < 15:
    time.sleep(1)
    print("Welcome, my child.")
    young_flag = True
    age_check = True
  elif user_age > 50:
    if user_age < 90:
      old_flag = True
      time.sleep(1)
      print("Oh ho, in those golden years of life?")
      time.sleep(1)
      print("How lovely.")
      age_check = True
    else:
      print("I don't like liars, goodbye.")
      quit()
  elif 25 <= user_age <= 50:
    adult_flag = True  
    time.sleep(1)
    print("So you are in the most eventful period of life?.")
    time.sleep(1)
    print("How lovely.")
    age_check = True
  else:
    ya_flag = True
    time.sleep(1)
    print("Oh, to be back in those glorious days of youth.")
    time.sleep(1)
    print("I almost envy you, dear.")
    age_check = True
  time.sleep(1)
print('So, you are "' + user + '" huh?')
time.sleep(1)
if len(user) == 1:
  print("Not much of a name, dear.")
  name_flag = True
elif 2 <= len(user) <= 5:
  print("Short and sweet, I like it.")
  name_flag = False
elif 6 <= len(user) <= 15:
  print("A lovely name.")
  name_flag = False
else:
  print("That is a long name.")
  name_flag = True
print("")
time.sleep(1)
while name_flag:
  name_answer = input("Do you wish to change it?[y/n]: ")
  if name_answer in ("y", "Y", "yes", "Yes", "YES"):
    name_changing = True  
    while name_changing:
      time.sleep(1)
      user = input("Tell me your real name," +
      " sweetie[only letters]: ")  
      if not user.isalpha():
        print("That is not a name.")
        print("")
      else:
        time.sleep(1)  
        print("Hello, " + user + ".")
        name_flag = False
        name_changing = False
  elif name_answer in ("n", "N", "no", "NO", "No"):
    name_flag = False
    time.sleep(1)
    print("Alright dearie, it is your decision.")
  else:
    print("Invalid response")
    print("")
print("Could you be a doll and fill out this form for me while we wait?")
sex_flag = False
while not sex_flag:
  sex = input('"Please write down your sex[F/M/X]": ')
  if sex in ("F", "X", "M"):
    sex_flag = True
    if sex == ("F"):
      f_flag = True
    elif sex == ("M"):
      m_flag = True
    elif sex == ("X"):
      x_flag = True
  else:
    print("Invalid response")
    print("")
hair_flag = False
while not hair_flag:
  hair_colour = input('"What colour is your hair?[Blonde/Brown/Black/Grey/White/Red/None]": ')
  if hair_colour in ("Blonde", "BLONDE", "blonde", "Brown", "BROWN", "brown", "Black", "BLACK", "black", "Grey", "Gray", "GREY", "GRAY", "grey", "gray", "White", "WHITE", "white", "Red", "RED", "red", "None", "none", "NONE"):
    hair_flag = True
  else:
    print("Invalid response")
    print("")
bmi_flag = False
while bmi_flag == False:
  height_flag = False
  while not height_flag:
    height = input('"How tall are you?[cm]": ')
    try:
      int(height)
    except:
      print("Invalid response")
      print("")
    else:
      height = int(height)  
      if height > 250 or height < 50:
        time.sleep(1)    
        print("I don't like liars (height).")
      else:
        height_flag = True
  weight_flag = False
  while not weight_flag:
    weight = input('"How much do you weigh?[kg]": ')
    try:
      int(weight)
    except:
      print("Invalid response")
      print("")
    else:
      weight = int(weight)  
      if weight > 500 or weight < 30:
        time.sleep(1)    
        print("I don't like liars (weight).")
      else:  
        weight_flag = True
  bmi = float(weight/(height*height*0.0001))
  if bmi < 10:
    print("I don't like liars (low bmi).")
  elif bmi > 80:
    print("I don't like liars (high bmi).")  
  else:
    bmi_flag = True  
time.sleep(1)
print("")
print("Here is your character profile, dearie.")
time.sleep(1)
print("")
print("CHARACTER PROFILE")
alt_profile_1 = False
alt_profile_2 = False
if young_flag and f_flag:
  descriptor = ("a young girl")
elif young_flag and m_flag:
  descriptor = ("a young boy")
elif young_flag and x_flag:
  descriptor = ("a child")
elif ya_flag and f_flag:
  descriptor = ("a young woman")
elif ya_flag and m_flag:
  descriptor = ("a young man")
elif ya_flag and x_flag:
  descriptor = ("a young adult")
elif adult_flag and f_flag:
  descriptor = ("a woman")
elif adult_flag and m_flag:
  descriptor = ("a man")
elif adult_flag and x_flag:
  descriptor = ("an adult")
elif old_flag and f_flag:
  descriptor = ("an older woman")
elif old_flag and m_flag:
  descriptor = ("an older man")
elif old_flag and x_flag:
  descriptor = ("an old timer")
if hair_colour in ("Blonde", "BLONDE", "blonde"):
  hair_colour = ("blonde")
elif hair_colour in ("Brown", "BROWN", "brown"):
  hair_colour = ("brown")
elif hair_colour in ("Black", "BLACK", "black"):
  hair_colour = ("black")
elif hair_colour in ("Grey", "Gray", "GREY", "GRAY", "grey", "gray"):
  hair_colour = ("grey")
elif hair_colour in ("White", "WHITE", "white"):
  hair_colour = ("white")    
elif hair_colour in ("Red", "RED", "red"):
  hair_colour = ("red")
elif hair_colour in ("None", "NONE", "none"):
  alt_profile_1 = True    
if height >= 190:
  height = ("very tall")
elif 190 > height >= 180:
  height = ("tall")
elif 180 > height >= 170:
  height = ("a person of average height")
elif 170 > height >= 160:
  height = ("short")
elif 160 > height >= 130:
  height = ("very short")
elif height < 130:
  height = ("tiny")
if bmi > 30:
 alt_profile_2 = True
 bmi = ("You have quite the excess of fat.")
elif bmi < 17:
 alt_profile_2 = True
 bmi = ("You are very thin.")
print("You are " + descriptor + " named " + user + ".")
if not alt_profile_1:
  print("You are " + height + " and your hair is " + hair_colour + ".")
else:
  print("You are " + height + ".")
if alt_profile_2:
  print(bmi)
reddit.com
u/ice_or_flames — 12 days ago