There are a few problems going on.
- 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
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
.
- 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))))
The function kiezen
isn't actually doing anything in your code. It is defined, but you don't use it anywhere, apparently.
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.
- 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.
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.
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()