Как начинающий, я пытался создать 1-строчный калькулятор с использованием Python ... И я пришел к этому. Я преобразовал пользовательский ввод в список, а затем использовал элементы списка, чтобы получить результат. Например, в случае умножения я взял элементы перед знаком, скомбинировал их, а затем элементы после знака и скомбинировал их. Затем умножьте все вместе.
question = list(input("Enter Your Question: "))
def convert(list):
s = [str(i) for i in list]
combined = int("".join(s))
list = combined
return(combined)
multiply = "*"
add = "+"
substract = "-"
divide = "/"
if multiply in question:
for multiply in question:
position1 = question.index("*")
before_num = question[0: int(position1)]
aft = len(question)
after_num = question[int(position1) + 1: int(aft)]
num_before = convert(before_num)
num_after = convert(after_num)
print(int(num_before) * int(num_after))
break
elif add in question:
for add in question:
position1 = question.index("+")
before_num = question[0: int(position1)]
aft = len(question)
after_num = question[int(position1) + 1: int(aft)]
num_before = convert(before_num)
num_after = convert(after_num)
print(int(num_before) + int(num_after))
break
elif substract in question:
for substract in question:
position1 = question.index("-")
before_num = question[0: int(position1)]
aft = len(question)
after_num = question[int(position1) + 1: int(aft)]
num_before = convert(before_num)
num_after = convert(after_num)
print(int(num_before) - int(num_after))
break
elif divide in question:
for divide in question:
position1 = question.index("/")
before_num = question[0: int(position1)]
aft = len(question)
after_num = question[int(position1) + 1: int(aft)]
num_before = convert(before_num)
num_after = convert(after_num)
print(int(num_before) / int(num_after))
break
else:
print("Check Your Question Again")
end_program_ans = input("Press Enter to continue")
Это работает отлично, но есть более простой способ.