I'm creating a program to do some mathematical accounts with some commands and have 1 line of code that repeats in all conditions, with only a small change in each condition. My question is how to create a function to call it instead of typing this line every time (I know it's not worth creating a function just to not repeat a line when sometimes I would even write more, but I think I'll need to learn to replace large chunks of code with functions). Here is my code and lastly the function I tried to create for it.
from script_calc import *
command = input()
if '!' in command:
a = int(command.strip('!')) # esse comando que eu gostaria de substituir todas as vezes que ele é repetido mudando só o caractere que está dentro do strip.
print(fact(a))
elif 'root' in command:
a = int(command.strip('root'))
print(root(a))
elif '²' in command:
a = int(command.strip('²'))
print(square(a))
elif '³' in command:
a = int(command.strip('³'))
print(cube(a))
The functions that were called:
from math import factorial
def fact(a):
return factorial(a)
def root(a):
return a ** (1/2)
def square(a):
return a ** 2
def cube(a):
return a ** 3
I tried to create this function to not repeat the command I said and gave error:
def same(x):
a = int(command.strip({}).format(x))
And then run it like this:
elif 'root' in command:
x = 'root'
same(x)
print(root(a))
What is the error in this function?