0

Explanation needed

https://code.sololearn.com/c3NiY3MtCRKY/?ref=app So, basically, I don’t understand a thing after “operation” string begins and etc. Could someone here make me the analysis, please? I would appreciate it very much

17th Feb 2020, 7:55 PM
Alisher Berdibekov
Alisher Berdibekov - avatar
5 Answers
+ 1
operation = [add,subtract,divide,multiply] options = ["+", "-", "/", "*"] When the user makes their choice (for this example, they choose "-"), the variable 'user_choice' is defined by options.index(choice). Now, 'choice' here is "-", so options.index("-") is 1. So 'user_choice' is defined as 1. The line you say you are having trouble understanding is math = operation [user_choice] (num1, num2) 'user_choice' is 1, so this line becomes math = operation [1] (num1, num2) Now, operation[1] is the second element of operation, which is subtract. So operation[1] returns subtract. The line now is effectively math = subtract(num1, num2) which is the function call for subtract() with the arguments 'num1' and 'num2'.
17th Feb 2020, 8:49 PM
Russ
Russ - avatar
+ 1
No problem
17th Feb 2020, 9:00 PM
Russ
Russ - avatar
0
i get the idea of def, it is easy but what comes after that - operation, options, choice and most importantly: math = operation [choice] (num1, num2). I just cannot get why
17th Feb 2020, 8:03 PM
Alisher Berdibekov
Alisher Berdibekov - avatar
0
god. it’s still quite hard, but you made it easier, thank you very much
17th Feb 2020, 8:56 PM
Alisher Berdibekov
Alisher Berdibekov - avatar