从Java转换为Python时遇到麻烦

时间:2019-03-26 20:56:16

标签: java python

因此,我一直在研究这段代码,以帮助正在参加他的第一个python课程的兄弟作为选修课(金融专业)。我以他用Java编写代码的想法帮助了他,以便我可以帮助他逐步了解python的逻辑。我没有python经验,只是想知道是否有人可以帮助转换第一个嵌套的if。我认为我们都没有得到python语法的问题,但是当我们仅在测试第二部分的条件下尝试仅在第二个条件下运行currentPrice时,就会遇到问题。非常感谢。

公共类FroyoShop {public static void main(String [] args){


    double currentPrice = 0.0;
    double iceCreamPrice = 3.49;
    double froyoPrice = 3.09;


    Scanner keyboard = new Scanner(System.in);
    System.out.println("Frozen Yogurt or Ice-Cream? (F: Frozen Yogurt, I: Ice Cream) ");
    String item = keyboard.nextLine();

        if (item.equals("f") || item.equals("F")){
            currentPrice = froyoPrice;

            System.out.println("What Size? (S: Small,M: Medium, L: Large)");
            String size = keyboard.nextLine();
            if(size.equals("s") || size.equals("S")){
                currentPrice = currentPrice;

            }
            else if (size.equals("m") || size.equals("M")){
                currentPrice = currentPrice + .8;
            }

            else if (size.equals("l") || size.equals("L")){
                currentPrice = currentPrice + 1.6; 
            }






        }
        else if (item.equals("i") || item.equals("I")){
            currentPrice = iceCreamPrice;

            System.out.println("What Size? (S: Small,M: Medium, L: Large)");
            String size = keyboard.nextLine();
            if(size.equals("s") || size.equals("S")){
                currentPrice = currentPrice;

            }
            else if (size.equals("m") || size.equals("M")){
                currentPrice = currentPrice + 1.0;
            }

            else if (size.equals("l") || size.equals("L")){
                currentPrice = currentPrice + 2.0; 
            }


            }

        System.out.println("What kind of container (1: Cone, 2: Cup) ");
        int type = keyboard.nextInt();
        if(type == 2){
            currentPrice = currentPrice + .1;
        }
        else if(type == 1){
            currentPrice = currentPrice;
        }
        System.out.println("Would you like to add a topping? (Y: Yes, N: No) ");
        String top = keyboard.next();
        if(top.equals("y") || top.equals("Y")){
            System.out.println("What kind of topping would you like? (G: Gummies, N: Nuts) ");
            String choice = keyboard.next();
            if(choice.equals("g") || choice.equals("G")){
                currentPrice = currentPrice + 1.59;

            }
            else if(choice.equals("n") || choice.equals("N")){
                currentPrice = currentPrice + 1.89;
            }
        }

        System.out.println("Would you like to add a cookie? (Y: Yes, N: No)");
        String cookie = keyboard.next();
        if(cookie.equals("y") || cookie.equals("Y")){
            System.out.println("What kind of cookie would you like? (R: Raisin, C:Chocolate)");
            String rOrC = keyboard.next();
            if(rOrC.equals("r") || rOrC.equals("R")){
                currentPrice = currentPrice + 1.49;
            }
            else if(rOrC.equals("c") || rOrC.equals("C")){
                currentPrice = currentPrice + 1.59;
            }
            System.out.println("What size of cookie would you like? (R: Regual, L: Large) ");
            String cookieSize = keyboard.next();
            if(cookieSize.equals("l") || cookieSize.equals("L")){
                currentPrice = currentPrice + 1;
            }
        }

        System.out.println("What day of the week is this? (M: Monday, T: Tuesday, W: Wednesday, Th: Thursday, F: Friday, SA: Saturday, SU: Sunday");
        String day = keyboard.next();
        if (day.equals("f") || day.equals("F") || day.equals("sa") || day.equals("SA") || day.equals("su") || day.equals("SU")){
            currentPrice = currentPrice - (currentPrice* .1);
        }


        System.out.println("Your order total is $" + currentPrice);
        }
    }

2 个答案:

答案 0 :(得分:0)

在python中,嵌套的if看起来像这样:

if ... :
    if ...  :
        dostuff()
    elif ... :
        morestuff()
 elif ... :
    if ...

...等等。 那是你要的吗?

答案 1 :(得分:0)

如果我了解您所讨论的代码的哪一部分,那么您需要这样的内容:

if (size.lower() == "s"):
    currentPrice *= 1.0
elif (size.lower() == "m"):
    currentPrice *= 0.8
elif (size.lower() == "l"):
    currentPrice *= 1.6

或更简洁易读的版本:

multiplier = {'s': 1.0, 'm': 0.8, 'l': 1.6}
currentPrice = currentPrice * multiplier[size.lower()]

对于实际应用程序,您需要首先检查输入值的有效性,以确保它是6个正确的字符串之一。如果输入其他内容,此代码将爆炸。

...,当然,从“世界如何运作”的意义上讲,乘数似乎并没有什么意义。