According to a recent survey, Biryani is the most ordered food. Chef wants to learn how to make world-class Biryani from a MasterChef. The chef will be required to attend the MasterChef's classes for X weeks and the cost of classes per week is Y coins. What is the total amount of money that the Chef will have to pay?

Input Format:

The first line of input will contain an integer T — the number of test cases.

The first and only line of each test case contains two space-separated integers.

for _ in range(int(input())):
        x,y = map(int, input().split())

I tried to solve this simple calculation problem with fewer declared variables. Is there any way to optimize the above code?

  • 2
    \$\begingroup\$ Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Please check that I haven't misrepresented your code, and correct it if I have. \$\endgroup\$ Commented Jun 28, 2023 at 8:29
  • 2
    \$\begingroup\$ In any case, reviewers are likely to prioritise readability over size, as this is not Code Golf. \$\endgroup\$ Commented Jun 28, 2023 at 8:30

1 Answer 1


Your code is concise, readable, and idiomatic. It works as you intended. It's small but nice snippet of code.

You could probably turn it into a one-liner, but it would get rather complex and readability would suffer, implying issues with maintainability, re-usability and reviewing.

Regarding style, I have two minor remarks:

  • The indentation is unusual, PEP 8 and recommends 4 spaces for indents, not 8. But this might as well be an issue with question formatting.
  • The variable names you chose, x and y, are undescriptive. I would prefer weeks and cost_per_week, although your approach of using the names in the problem description is understandable.

I have a bigger issue with your use of input for getting data. From what I understand from the input format description, I expect the format to be a text file. Expecting the user to copy, line by line, the input data fails to solve the problem in my book. Python makes it quite easy to work with files and iterating over lines. You should look into the open built-in function and work with that.

In any case, when working with user input, you should validate that input and handle invalid input gracefully. Imagine mis-typing a line on the 99-th test case out of 100, your code would crash and the user would need to start over from the top. You can expect an input file to be properly formatted, but that assumption doesn't hold for data gotten through input.

The use of print for output is also sub-optimal, as you would like to validate test cases programmatically. However, in this case, since the test data doesn't provide expected values, it is a decent approach, for lack of a better solution.

  • \$\begingroup\$ Any reference book on coding style or blogs? \$\endgroup\$
    – kvk30
    Commented Jun 30, 2023 at 15:17
  • \$\begingroup\$ For style, PEP 8 (linked in my answer) is a very good read. Some of it is Python-specific, but a lot is good insight for all code. \$\endgroup\$
    – gazoh
    Commented Jun 30, 2023 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.