0

At school we have to write a program to use the pythagorean theory. Im writing it in python 3 but when I return cber the program just ends. bber on the other hand works fine. Can someone please help? Thanks already:)

Edit: Thanks for helping me, this is not everything with the function kiezen the user can choose two numbers and the j and the n are to decide which lines they are in the triangle, thats in the function kiezen too. This is all in one function called cijfers, I dont know if that makes a difference. I used return because that way I could let the user choose the numbers again if he/she entered something unvalid. And I forgot to remove the ifs in cber before posting it. I’ll try to improve my program sometime soon. Thanks for all your feedback:)

 def bber():
   if (c >= a):
     print(str(a) + "^2 + b^2 = " + str(c) + "^2")
     print("b^2 = " + str(c) + "^2 - " + str(a) + "^2")
     print("b = V(" + str(c**2) + " - " + str(a**2) + ")")
     print("b = V" + str((c**2) - (a**2)) + " = " + str(math.sqrt((c**2) - (a**2))))
   if (a >= c):
     print("De rechthoekzijde kan niet langer zijn dan de schuine zijde.")
     cijfers()
 def cber():
   if (a >= b):
     print(str(a) + "^2 + " + str(b) + "^2 = c^2")
     print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
     print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
     print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))
   if (b >= a):
     print(str(a) + "^2 + " + str(b) + "^2 = c^2")
     print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
     print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
     print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))

 def kiezen():
   x = int(input("Wat is de lengte van de eerste zijde?"))
   xz = input("Is deze zijde een rechthoekzijde (J/N)?")

   print(" ")

   y = int(input("Wat is de lengte van de tweede zijde?"))
   yz = input("Is deze zijde een schuine zijde (J/N)?")

   print(" ")

 return kiezen()

 if xz == "j" or "J":
   if yz == "n" or "N":
      b = y
      a = x
      return cber()
   if yz == "j" or "J":
     c = y
     a = x
     return bber()
  • 1
    Just a hint (I don't know if that is the cause of the problem): the statement `if yz == "j" or "J":` gets interpreted as `if (yz == "j") or ("J"):`. That is probably not what you want. – Ralf Dec 20 '18 at 19:59
  • 1
    Also, could you please fix the intentations of your code? I cannot identify where the last lines belong to, because they contain `return` statements but not clear starting `def`. – Ralf Dec 20 '18 at 20:04
  • See [how do I teset one variable against multiple values](https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – Barmar Dec 20 '18 at 20:40
  • You should learn to use function parameters instead of global variables. – Barmar Dec 20 '18 at 20:41
  • 1
    What is `return kiezen()` supposed to do? It's not inside a function, so what are you returning from? And `kiezen()` doesn't return anything, so what are you trying to return? And this statement will prevent the `if` statement after it from running. – Barmar Dec 20 '18 at 20:42
  • 1
    the same question about `return bber()` and `return cber()`. Those functions don't return anything, so why are you returning their values? – Barmar Dec 20 '18 at 20:44

1 Answers1

0

There are a few problems going on.

  1. You need to import modules.

In your code, you are using math.sqrt, so the first line required is to actually import the math module at the beginning of your file:

import math
  1. You are not getting access to your variables inside your functions. To pass them to your function, you have to specify them as function parameters:

    def bber(a, c):

On the positive side, your function bber reports the right answer inside your statement if (c >= a). However, the following conditional statement, if (a >= c), calls the function cijfers(), which doesn't actually exist. In that case, every time a is greater than or equal to c, the program will print a NameError.

  1. The function cber works, but you don't actually need to have if statements, because you get the variable c no matter if b is greater than a or if a is greater than b. You might want to consider checking for other types of input (like text, negative numbers, floats, etc.), though.

Here is how you could simplify your cber function, also having to pass in actual parameters:

def cber(a, b):
        print(str(a) + "^2 + " + str(b) + "^2 = c^2")
        print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
        print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
        print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))
  1. The function kiezen isn't actually doing anything in your code. It is defined, but you don't use it anywhere, apparently.

  2. Variables defined inside functions are local to that function while variables defined outside a function (i.e. no indentation) are global variables. When you need to use a global variable inside a function, you have to pass it as a function parameter. For more information on this topic, you can read about the concepts of "scope", "global scope" and "local scope". You can also find examples in the official Python documentation, here.

For now, so you can see how to use variables that are defined globally, I will use your incomplete kiezen function without actually making it a function, so the code is directly executed in your program.

  1. One other problem here is that you can use the keyword return only inside a function, because that is what it is meant for: to return the result of a function.

This means that you have to change your code return cber() and return bber() by removing the return keyword.

  1. You are missing a space at the end of your questions with input. When you type your answer, it will appear right next to the last character in your string.

  2. When you want to check for multiple options (as you did with XZ == "j" or "J"), you can use a list instead and the keyword in.

Here are some modifications needed to make your program work in the end. Comments are present on lines with the # symbol.

# To use a function provided by the math module, you have to import it first
import math


# You have to get "a" and "c" from somewhere, so you pass them as parameters
def bber(a, c):
    if (c >= a):
        print(str(a) + "^2 + b^2 = " + str(c) + "^2")
        print("b^2 = " + str(c) + "^2 - " + str(a) + "^2")
        print("b = V(" + str(c**2) + " - " + str(a**2) + ")")
        print("b = V" + str((c**2) - (a**2)) + " = " + str(math.sqrt((c**2) - (a**2))))

    if (a >= c):
        print("De rechthoekzijde kan niet langer zijn dan de schuine zijde.")


# Same scenario here: "a" and "b" must be defined somehow
# Note that the "if" statements were unnecessary
def cber(a, b):
        print(str(a) + "^2 + " + str(b) + "^2 = c^2")
        print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
        print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
        print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))

# Note that a space has been added at the end of each string
# where you use "input".
X = int(input("Wat is de lengte van de eerste zijde? "))
XZ = input("Is deze zijde een rechthoekzijde (J/N)? ")

print(" ")

Y = int(input("Wat is de lengte van de tweede zijde? "))
YZ = input("Is deze zijde een schuine zijde (J/N)? ")

print(" ")

if XZ in ["j", "J"]:
    if YZ in ["n", "N"]:
        b = Y
        a = X

        # You have to remove the word "return", this is not a function
        cber(a, b)

    if YZ in ["j", "J"]:
        c = Y
        a = X

        # You have to remove the word "return", this is not a function
        bber(a, c)

As I mentioned earlier, this is not perfect because you would need to manage errors. For example, it will not work if you enter text instead of numbers, but that is a topic for another discussion.

Also, for the purpose of this exercise, you do not actually "need" to use the return keyword in your functions if your goal is just to print the output, but keep in mind that if you need to reuse the result of a function, you will have to return a value for it.

A quick example of that concept would be the following:

def my_function():
    return 12

# my_variable will have a value of 12
my_variable = my_function()
Sébastien Lavoie
  • 877
  • 1
  • 11
  • 18