Answer the question
In order to leave comments, you need to log in
Is it possible in Python to make multilevel if / how to make a puzzle with Stepik calculator?
b = float(input())
x = input()
if x == "+" :
print(a+b)
elif x =="-" :
print(a-b)
elif x =="*" :
print(a*b)
elif x =="pow" :
print(a**b)
elif x=='/':
if b=='0':
print('Деление на 0!')
else:
print(a/b)
elif x=='mod':
if b=='0':
print('Деление на 0!')
else:
print(a//b)
elif x=='div':
if b=='0':
print('Деление на 0!')
else:
print(a%b)
# не робит це
a = float(input())
Answer the question
In order to leave comments, you need to log in
def calc(a,b,op):
if op in ['/', 'mod', 'div'] and b == 0:
return 'Деление на 0!'
else:
return({
'+': a+b,
'-': a-b,
'*': a*b,
'/': a/b,
'mod': a//b,
'div': a%b,
}.get(op, 'Неизвестная операция!'))
print(calc(1,2,'+'))
print(calc(1,2,'/'))
print(calc(2,0,'mod'))
print(calc(0,2,'$'))
3
0.5
Деление на 0!
Неизвестная операция!
yes, but i would do that
import operator
action = {
"+": operator.add,
"-": operator.sub,
"/": operator.truediv,
"*": operator.mul,
"**": operator.pow
}
# .....
b = float(input())
a = float(input())
x = input()
act= action.get(x, None)
if act:
try:
answer = act(a, b)
except ZeroDivisionError as e:
print(u"Деление на 0!")
else:
print(answer)
def addition(a, b):
return a + b
def subtraction(a, b):
return a - b
operations = {
'+': addition,
'-': subtraction
}
print(operations['+'](5, 7));
print(operations['-'](9, 4));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question