0
print("Calculadora 2")
task = input("Choose a task (+, -, *, /): ")
n1 = input("Number 1: ")
n2 = input("Number 2: ")

print(float(n1 + task + n2))

When I try to run this code it says: ValueError: could not convert string to float: '2+3' How can I make it not a string and a number(float).

Eyyt4
  • 1

1 Answers1

1

Here, eval can be very useful. However, use it cautiously:

print("Calculadora 2")
task = input("Choose a task (+, -, *, /): ")
n1 = input("Number 1: ")
n2 = input("Number 2: ")

print(eval(n1+task+n2))
  • thanks, but why the "use it cautiously", I am kinda curious now – Eyyt4 Jul 05 '21 at 12:27
  • [Why is using ```eval``` dangerous](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice). Check it out @Eyyt4 –  Jul 05 '21 at 12:28