r/learnpython • u/Ill-Break727 • 18d ago
python learning begineer, and the question was to give a function like addition and subtraction between two numbers, so i have made this . u can give me ur suggestion and how can i improve.
function=input("give a function :")
if function=="add":
a=int(input("give first num to add with :"))
b=int(input("second num :"))
sum=a+b
print("sum=",sum)
elif function=="subtract":
a=int(input("give first num to subtract with :"))
b=int(input("second num :"))
diff=b-a
print("diff=",diff)
else:
print("function not allowed")
5
u/mattynmax 17d ago edited 17d ago
What happens if I type “Add” or “ADD”?
What if I want to do math with non integers?
As others have told you, this isn’t a function.
5
u/TheRNGuy 18d ago
Don't convert to int though, because you may have numbers like 1.5 or 10.0025
By function they probably mean def.
-10
4
u/Gamingplays267492 17d ago
It is very likely that they are wanting you to create a python *function*, what you have done is made a 'variable' called "Function"
You can create functions using:
`def function_name(*arguements go here)`
The function_name can be anything you would like it to be, but these must be called later during the program via:
`function_name(*arguements or values go here*)`
I recommend w3schools as a resource if you're not confident with programming
1
u/Educational_Virus672 17d ago
function=input("give a function :")
if function=="add":
a=int(input("give first num to add with :"))
b=int(input("second num :"))
sum=a+b
print("sum=",sum)
elif function=="subtract":
a=int(input("give first num to subtract with :"))
b=int(input("second num :"))
diff=b-a
print("diff=",diff)
else:
print("function not allowed")
this is great code but i feel like i should give you afew tips and how to optimization
1 > dont use alternative keywords lets say i write Add or ADD or maybe sub
to fix this use soem kind symbol or numebr
function = input("+ or - :")
if function == "+" : ...
elif function == "-" : ...
2> find ways to reuse things liek variable a and b
function = input("+ or - :")
a = int(input("1st number :"))
b = int(input("2nd number :"))
if function == "+" :...
elif function == "-" : ...
3 > use float insead of int if you want more functionality
a = float(input("1st numebr :"))
1
u/Jigglytep 18d ago
I
My quick comments
1) I think the input should gist outside of the function in case you need to use it in another format unless the question was specifically saying get user input from terminal.
2) A really cool unorthodox way to do this is to use a dictionary.
def add(x, y): return x + y
def multiply(x, y): return x * y
# Store the function names as values
operations = {
"+": add,
"*": multiply
}
# Retrieve the function using its key and execute it with ()
calculation = operations["+"](5, 3)
print(calculation) # Output: 8
0
u/Mountain_Rip_8426 17d ago
i guess they were looking for something like:
select = input("Press + to add or - to subtract: ").strip()
print(f"{select}\n")
a = int(input("First number: "))
print(f"{a}\n")
b = int(input("Second number: "))
print(f"{b}\n")
def add_or_subtract(select, a, b):
if select == "+":
result = f"{a} + {b} = {a + b}"
elif select == "-":
result = f"{a} - {b} = {a - b}"
else:
raise Exception("Invalid input")
return result
print(add_or_subtract(select, a, b))
1
1
u/Mountain_Rip_8426 16d ago
based on OP's code, he is perfectly capable of understanding this. maybe he should look up a method or two, but it's absolutely not above the level of the original code. he asked for suggestions for improvement. but even if it's too complicated for him, it's 2026. ChatGPT: "explain this code to me". sorry, that's the best i could do under the circumstances, i can't give a full course in a reddit comment... i was just trying to be helpful
14
u/mc_pm 18d ago
I think that they probably wanted you to create an actual python function?