+ 6

Can please anybody show me the bug in the code

I am trying to create a menu based calculator but the code keeps bring an error This part of code it us not yet finished. Here is the code. result = 0 def main(): print("1. Basic Math") print("2. Advanced Math") choice = input("enter choice: ") if choice == "1": result = mat() elif choice == "2": result = advanced() result = main() def mat(): print("1. Addition") print("2. Subtraction") print("3. Multplication") print("4. Remainder") print("5. Division") choice2= input("enter choice: ") if choice2 == "1": result = add() elif choice2 ==" 2": result = sub() elif choice2 == "3": result = mult() elif choice2 == "4": result = rem() elif choice2 == "5": result = div() result = mat() def add(): num1 = int(input("Enter no 1: ")) num2 = int(input("Enter no 2: ")) print(num1 + num2) result = add()

13th Jul 2021, 9:32 PM
MATOVU CALEB
MATOVU CALEB - avatar
8 Antworten
+ 3
add() is not a built-in function of Python. I think he is thinking of the magic method __add__() which belongs to an object. Your issue is that you're calling function prior to them being defined. Python files are read by the interpreter from left to right top down. Your calling main() prior to the rest of the functions being defined, likewise with the add function. You can either rearrange your code so that the functions are above main() or you can move the function call below the point in which is subsequent function call is made so that they are all defined in the file first. You don't need to be setting any of the function calls to a variable, as none of your functions return any values. Keeping the call to main() as the last line will fix the issue. Example code; https://code.sololearn.com/csW9YLCYMcM9/?ref=app
14th Jul 2021, 3:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
U should define the function before calling or using it And u should put else in your code and complete it
13th Jul 2021, 9:38 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 4
Muhammad Thanks for your help. Let me try and see.
13th Jul 2021, 9:39 PM
MATOVU CALEB
MATOVU CALEB - avatar
+ 3
You're welcome
13th Jul 2021, 9:44 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 2
ChaoticDawg Thanks for your help The code now works properly.
14th Jul 2021, 9:13 AM
MATOVU CALEB
MATOVU CALEB - avatar
+ 1
It looks like you are calling matovu before you have defined it. If you move the def matovu () block above the def main () block, you should be fine.
15th Jul 2021, 10:53 AM
Luke Morrison
0
Muhammad I have changed the code but now it brings an error that matovu is not defined result = 0 def main(): print("1. Basic Math") print("2. Advanced Math") choice =int( input("enter choice: ")) if choice == 1: matovu() elif choice == 2: result = advanced() result = main() def matovu(): print("1. Addition") print("2. Subtraction") print("3. Multplication") print("4. Remainder") print("5. Division") choice2= int(input("enter choice: ")) if choice2 == 1: result = new() elif choice2 == 2 : result = sub() elif choice2 == 3: result = mult() elif choice2 == 4: result = rem() elif choice2 == 5: result = div() result = matovu() def new(): num1 = int(input("Enter no 1: ")) num2 = int(input("Enter no 2: ")) print(num1 + num2) result = new()
13th Jul 2021, 9:51 PM
MATOVU CALEB
MATOVU CALEB - avatar
0
Luke Morrison Thanks for notifying it
15th Jul 2021, 2:26 PM
MATOVU CALEB
MATOVU CALEB - avatar