簡體   English   中英

如何優化此代碼以使其不那么笨重

[英]How to optimise this code to not be so chunky

這是一個計算器,它需要 3 個輸入,即團體規模、捐款金額和產品數量。 我的問題是我所做的不必要的代碼和重復的數量。 我花了一段時間試圖最小化它以擺脫手動執行每個不同組大小的需要,但在進入循環時我似乎有一個思考障礙。

我的問題是如何優化此代碼以消除所有重復。 每次需要計算下一個人的數量時,您如何使用 while 循環或任何方法分配不同的變量。

它的限制太六了,因為如果我想讓它更多,那就是另一塊。

提前致謝。


"\nHow many people are buying? MAX 6: "))

    
Gs = float(input("How many grams are you picking up?: "))
Ps = float(input("How much money is being paid?: "))
PPG = Ps / Gs
print ("Price per gram is:" ,"£",PPG,)


def MainCalc():
    if GroupNo == 6:
        P1 = int(input("How much £ is person 1 putting in: "))
        P2 = int(input("How much £ is person 2 putting in: "))
        P3 = int(input("How much £ is person 3 putting in: "))
        P4 = int(input("How much £ is person 4 putting in: "))
        P5 = int(input("How much £ is person 5 putting in: "))
        P6 = int(input("How much £ is person 6 putting in: "))
        P1_Gs = P1 / PPG  
        print("Person 1 should get",P1_Gs,)
        P2_Gs = P2 / PPG  
        print("Person 2 should get",P2_Gs,)
        P3_Gs = P3 / PPG  
        print("Person 3 should get",P3_Gs,)
        P4_Gs = P4 / PPG  
        print("Person 4 should get",P4_Gs,)
        P5_Gs = P5 / PPG  
        print("Person 5 should get",P5_Gs,)
        P6_Gs = P6 / PPG  
        print("Person 6 should get",P6_Gs,)
    elif GroupNo == 5:
        P1 = int(input("How much £ is person 1 putting in: "))
        P2 = int(input("How much £ is person 2 putting in: "))
        P3 = int(input("How much £ is person 3 putting in: "))
        P4 = int(input("How much £ is person 4 putting in: "))
        P5 = int(input("How much £ is person 5 putting in: "))
        P1_Gs = P1 / PPG  
        print("Person 1 should get",P1_Gs,)
        P2_Gs = P2 / PPG  
        print("Person 2 should get",P2_Gs,)
        P3_Gs = P3 / PPG  
        print("Person 3 should get",P3_Gs,)
        P4_Gs = P4 / PPG  
        print("Person 4 should get",P4_Gs,)
        P5_Gs = P5 / PPG  
        print("Person 5 should get",P5_Gs,)
    elif GroupNo == 4:
        P1 = int(input("How much £ is person 1 putting in: "))
        P2 = int(input("How much £ is person 2 putting in: "))
        P3 = int(input("How much £ is person 3 putting in: "))
        P4 = int(input("How much £ is person 4 putting in: "))
        P1_Gs = P1 / PPG  
        print("Person 1 should get",P1_Gs,)
        P2_Gs = P2 / PPG  
        print("Person 2 should get",P2_Gs,)
        P3_Gs = P3 / PPG  
        print("Person 3 should get",P3_Gs,)
        P4_Gs = P4 / PPG  
        print("Person 4 should get",P4_Gs,)
    elif GroupNo == 3:
        P1 = int(input("How much £ is person 1 putting in: "))
        P2 = int(input("How much £ is person 2 putting in: "))
        P3 = int(input("How much £ is person 3 putting in: "))
        P1_Gs = P1 / PPG  
        print("Person 1 should get",P1_Gs,)
        P2_Gs = P2 / PPG  
        print("Person 2 should get",P2_Gs,)
        P3_Gs = P3 / PPG  
        print("Person 3 should get",P3_Gs,)
    elif GroupNo == 2:
        P1 = float(input("How much £ is person 1 putting in: "))
        P2 = float(input("How much £ is person 2 putting in: "))
        P1_Gs = P1 / PPG 
        print("Person 1 should get",P1_Gs,)
        P2_Gs = P2 / PPG 
        print("Person 2 should get",P2_Gs,)
    elif GroupNo == 1:
        print("It only you, why are you using this ?")
    elif GroupNo > 6:
        print("Too many man, Please try a number below 7")


    

MainCalc()     ```

試試這個。 您需要研究的主要技術是for loops 它可以通過其他方式完成,但這個非常簡單且易於理解:

GroupNo = int(input("How many people are buying?: "))
Gs = float(input("How many grams are you picking up?: "))
Ps = float(input("How much money is being paid?: "))
PPG = Ps / Gs
print ("Price per gram is:" ,"£",PPG,)

def MainCalc():
    if GroupNo == 1:
        print("It's only you, why are you using this?")
    elif GroupNo < 7:
        allMoney = []
        for i in range(GroupNo):        
            allMoney.append(int(input(f"How much £ is person {i+1} putting in: ")))
        for person, money in enumerate(allMoney):
            print(f"Person {person+1} should get: {money/PPG}")  
    else:
        print("Too many people")
    
MainCalc()

Output:

How many people are buying?: 5
How many grams are you picking up?: 6
How much money is being paid?: 7
Price per gram is: £ 1.1666666666666667
How much £ is person 1 putting in: 8
How much £ is person 2 putting in: 3
How much £ is person 3 putting in: 5
How much £ is person 4 putting in: 4
How much £ is person 5 putting in: 1
Person 1 should get: 6.857142857142857
Person 2 should get: 2.571428571428571
Person 3 should get: 4.285714285714286
Person 4 should get: 3.4285714285714284
Person 5 should get: 0.8571428571428571

您可以研究此代碼以了解有關循環的一些信息:

PPG = 20.34   # Or whatever the proce per gram is.

while True:
    payment = float(input("Enter amount offered: "))
    if payment == 0:  # Quit when the amount is zero
        break
    print('you paid: £',  payment, 'so you get:', payment/PPG, 'grams')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM