I just wanted to ask something if I may... For the input initially I wasn't sure how I could input 2 numbers and make it work like a division...so I researched and came across this method:
a, b = [int(a) for a in input("Fraction: ").split('/')]
I get most part of it...
a and b are variables where the input is stored...
int(a) converts the input to a number ... but how about b?
for a in input .... not quite sure about this part?
.split('/') splits the string in 2 string values
[ ] I guess by using these brackets we want to store it as a dictionary but for what? for the loop to work?
If you could clarify my questions I would really appreciate...
Many thanks
I love learning math (*to some extent*), CS theory, good literature, and the technology that provides me with all kinds of resources for them: the web.
int(a) converts the input to a number ... but how about b?
This piece of code (a, b = [int(a) for a in input("Fraction: ").split('/')]) is actually a list comprehension — one of my favorites of Python. It is just a more elegant, and "pythonic" way to write for loops. Here, it asks for an input, and splits it with /, then it puts the values (converted to integers) inside a list. Then it unpacks the list with assigning the two values into variables a and b. Literally the same as this:
So, the a inside the list is different from a the variable that the first value is assigned to. Giving them the same name is not a good practice in my opinion. You can change that name and it does not matter, for example, a, b = [int(x) for x in input("Fraction: ").split('/')] gives the same output.
W3Schools has a basic explanation on how list comprehensions work. You can also read about them from the official docs.
Glad you passed the tests. The whole aim of the infinite loop is just to keep getting input, but using that list comprehension example inside an infinite loop would continue asking for an input forever and keep adding the results into a list, so it wouldn't work. I think it's better to convert the values after you got the input, but it really depends on how you would like to implement it.
I just wanted to ask something if I may... For the input initially I wasn't sure how I could input 2 numbers and make it work like a division...so I researched and came across this method:
a, b = [int(a) for a in input("Fraction: ").split('/')]
I get most part of it...
a and b are variables where the input is stored...
int(a) converts the input to a number ... but how about b?
for a in input .... not quite sure about this part?
.split('/') splits the string in 2 string values
[ ] I guess by using these brackets we want to store it as a dictionary but for what? for the loop to work?
If you could clarify my questions I would really appreciate...
Many thanks
This piece of code (
a, b = [int(a) for a in input("Fraction: ").split('/')]
) is actually a list comprehension — one of my favorites of Python. It is just a more elegant, and "pythonic" way to write for loops. Here, it asks for an input, and splits it with/
, then it puts the values (converted to integers) inside a list. Then it unpacks the list with assigning the two values into variablesa
andb
. Literally the same as this:So, the
a
inside the list is different froma
the variable that the first value is assigned to. Giving them the same name is not a good practice in my opinion. You can change that name and it does not matter, for example,a, b = [int(x) for x in input("Fraction: ").split('/')]
gives the same output.W3Schools has a basic explanation on how list comprehensions work. You can also read about them from the official docs.
Glad you passed the tests. The whole aim of the infinite loop is just to keep getting input, but using that list comprehension example inside an infinite loop would continue asking for an input forever and keep adding the results into a list, so it wouldn't work. I think it's better to convert the values after you got the input, but it really depends on how you would like to implement it.
Thanks a lot for the clear explanation...