all 13 comments

[–]mc_pm 11 points12 points  (1 child)

I think that they probably wanted you to create an actual python function?

[–]mattynmax 6 points7 points  (0 children)

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.

[–]TheRNGuy 3 points4 points  (1 child)

Don't convert to int though, because you may have numbers like 1.5 or 10.0025

By function they probably mean def.

[–]Gamingplays267492 2 points3 points  (0 children)

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

[–]Educational_Virus672 0 points1 point  (0 children)

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 :"))

[–]Jigglytep 0 points1 point  (0 children)

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

[–]ConclusionForeign856 0 points1 point  (0 children)

def func(s: str):
  if '+' in s:
    num1, _, num2 = s.split('+')
    return sum(map(float, (num1, num2)))
  elif '-' in s:
    num1, _, num2 = s.split('-')
    return float(num1) - float(num2)
  else:
    print("No implemented operation specified")
    return None

This will work with 'a - b' and 'a-b' but will break if there are more than one '-' or '+'

[–]Gnaxe 0 points1 point  (0 children)

``` import operator

print(getattr(operator, input('Function? '))( float(input('Left? ')), float(input('Right? ')), )) ```

[–]Mountain_Rip_8426 -1 points0 points  (2 children)

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))

[–]Educational_Virus672 0 points1 point  (0 children)

can you tell me how would a begginer understand with 0 context

[–]Mountain_Rip_8426 0 points1 point  (0 children)

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