-5

Here I have attached the PHP code, which consists of a switch case. I am in need that switch case in python

public function partsToUnits($totalParts, $partsInUnit = 30, $flagRound = 'ceil')
    {
        switch ($flagRound) {
            case 'floor':
                $totalUnits = (int) floor($totalParts / $partsInUnit);
                break;
            case 'ceil':
            default:
                $totalUnits = (int) ceil($totalParts / $partsInUnit);
                break;
        }

        $restParts  = fmod($totalParts, $partsInUnit);

        return array ('units' => $totalUnits, 'parts' => $restParts);
    }

What is the equivalent statement I can use in python?

  • 1
    SO is not a code conversion service. You are suppose to do the research and write the code yourself. We can help out if you run into some _specific_ issue with your _existing_ code. – M. Eriksson Dec 07 '18 at 07:38
  • 1
    Hem....you have to code it by yourself....nobody code for you – Sfili_81 Dec 07 '18 at 07:38
  • Just rewrite it as an `if` statement, which is really what a switch does. – Burhan Khalid Dec 07 '18 at 07:40
  • Possible duplicate of [Replacements for switch statement in Python?](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – Igor Ilic Dec 07 '18 at 07:41
  • Sorry guys please remove the negative scores I got minus marks – Aravindh Thirumaran Dec 07 '18 at 07:48
  • 1
    _"please remove the negative scores"_ - Off-topic questions gets negative scores. To prevent this in the future, please read through [the guidelines](https://stackoverflow.com/help/asking). – M. Eriksson Dec 07 '18 at 07:51

1 Answers1

-1

In python there is the if .... elif .... elif....else It is the switch in other languages.

But in your code you do not need a switch statement. It's enough to use a if then else.

if flagRound == floor:
    totalUnits = floor(totalParts / partsInUnit)
else:
    totalUnits = ceil(totalParts / partsInUnit)
robob
  • 1,739
  • 4
  • 26
  • 44
  • 1
    Please don't answer off-topic questions. Doing that might make the OP and future visitors keep asking off-topic questions. – M. Eriksson Dec 07 '18 at 07:44
  • I agree with you that the question is very simple but for me is not OT. Please consider the downgrade to my answer. – robob Dec 07 '18 at 07:54
  • Since the OP shows no research effort or attempt in any way shape or form to solve this them selves, the question is off-topic. That's not just my opinion, that's even one of the reasons for down voting the original question that's popping up when hovering the down vote button. However, I'm not the down voter on this answer. – M. Eriksson Dec 07 '18 at 07:58