Use Python conditional if

Asked 1 years ago, Updated 1 years ago, 158 views

Enter a formula (*for example, 20*40): 21.1 + 30.0

21.100000 + 30.000000 = 51.100000

a,b=input ("Enter a formula (*for example, 20*40):")split()

I know how to write an input function in this form, but I don't know how to handle + in 21.1+30.0.

if-else input if문

2022-09-22 10:18

1 Answers

After receiving a binary equation using the input function, are you trying to put each term except for the operator in a and b?

The result of input() comes in str format, and when str is divided by split(), the list of str is returned. When you divide by split(), if you don't give any parameters, you divide them by space.

So, if you take the 21.1000 + 30.000 input in the way that you write it down above, you'll see an Orgel called too many values to unpack. This means that you can't put a list in both variables, because dividing the input string by space results in a list with three elements: '21.1000', '+', and '30.000'.

A slight modification of the above source can cause a and b to have their respective values.

a, op, b=input ("Enter formulas (*for example, 20*40):").split()

So a has 21.1000, b has 30.000, and op has +.


2022-09-22 10:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.