0

These two codes have only one difference in them. The first one has a * in it and the second one dosen't. The first code runs properly and the second code gives this error:

TypeError: argument should be a string or a Rational instance

Code 1: The correct code.

from fractions import Fraction

fracs = []
for _ in range(int(input())):
    fracs.append(Fraction(*map(int, input().split())))

print(fracs)

Code2 : The errorneous code.

from fractions import Fraction

fracs = []
for _ in range(int(input())):
    fracs.append(Fraction(map(int, input().split())))

print(fracs)

I had learn python from Multiple sites like 'GeeksForGeeks' and 'Tutorial Point'. I checked and they don't tell what does this * does apart from being used as a multiplier,string-repeater and passing multiple argument.

Link to the page: https://www.tutorialspoint.com/What-does-the-Star-operator-mean-in-Python

Also, searched for this in stackoverrflow's previously asked question but it doesn't give any satisfying result.

Thanks in advance.

Ash
  • 375
  • 1
  • 3
  • 14
  • it passes all the values in the returned tuple as arguments to the function – dangee1705 Mar 05 '20 at 15:59
  • `*` here unpacks the arguments. It's the difference between `Fraction([1, 2, 3])` (well, a generator actually, not a list, but that's hard to visualise) and `Fraction(1, 2, 3)`. – deceze Mar 05 '20 at 16:00

0 Answers0