2017-09-21 92 views
0

我已經經歷了C++,現在正在學習Python。我對這裏發生的變化有點困惑。我覺得我應該將「wt」聲明爲Real或Float,但不會接受該語法。我認爲錯誤是在我聲明模塊「calcAndDisplayShipping」的行上。python中的變量定義

該計劃的目標是根據輸入的重量來計算價格。

#main module 
def main(): 

    #local variables 
    weight = 0.0 

    #get package weight 
    weight = input("Enter the weight of your package: ") 

    #call module to calculate and display shipping charges 
    calcAndDisplayShipping (weight) 

#module for calculating and displaying shipping charge 
def calcAndDisplayShipping (wt): 

    #named constants for rates 
    underTwo = 1.10 
    twoToSix = 2.20, 
    sixToTen = 3.70 
    overTen= 3.80 

    #Local Variable 
    shipping = 0.0 

    #calculate charges 
    if wt > 10.0: 
     shipping = wt * overTen 
    elif wt > 6.0: 
     shipping = wt * sixToTen 
    elif wt > 2.0: 
     shipping = wt * twoToSix 
    else: 
     shipping = wt * underTwo 

    #display shipping charge 
    print ("Shipping charge for this package is: $", shipping) 

    #return to main 
    main() 

我得到的錯誤是... 類型錯誤:「>」

我通過我的語言的同伴搜索蟒蛇,但沒有找到「STR」和「浮動」的實例之間不支持任何幫助。

+0

只需將wt轉換爲浮點數(float(wt))即可。如果用戶輸入非數字值,您將需要一些異常處理。 –

+0

我認爲這適用於Python2 ... Python3爲您提供了一個字符串,即使您似乎認爲您鍵入了一個數字 –

+0

同樣重要的是要注意Python不像C++那樣是動態類型的,因此您不需要聲明變量類型。 –

回答

0

在Python 3,input返回一個字符串。要獲得浮動點,您應該將此調用包裝在float()中。

weight = input("Enter the weight of your package: ") 

有可能會碰到這裏,因爲這是目前寫的,你永遠不會走出一個函數調用另外一個問題 - 你只會增加您的調用堆棧的大小。相反,你可能想在calcAndDisplayShipping末刪除呼叫main,然後在main使用while循環。

def main(): 

    #local variables 
    weight = 0.0 

    while True: 
     #get package weight 
     weight = float(input("Enter the weight of your package: ")) 

     #call module to calculate and display shipping charges 
     calcAndDisplayShipping(weight) 
+0

或'calcAndDisplayShipping(float(weight))' –

+0

我很確定OP希望它是float而不是int。 – abccd

+0

權利,浮動是正確的。 –

0

這裏有幾個問題。主要的一點是,在分配給weight之前,不要將字符串轉換爲浮點數。你可以這樣做:

weight = float(input(... 

在此之前,您不需要設置weight = 0.0。它被完全覆蓋。

的其他問題是,你的縮進是錯誤的,「計算費用」片段是功能之外。它將作爲模塊代碼塊的一部分運行。

您的評論的命名也是不正確的。你正在定義函數,而不是模塊。 (main,calcAndDisplayShipping)同樣在最後你打電話給main,而不是回到它。

最後這不會做你所期望的:

twoToSix = 2.20, 

它定義了一個1元的元組,相當於twoToSix = (2.20,)。您需要刪除逗號才能獲取號碼本身。

0

因爲Python 3.x中不評估和轉換數據類型,你必須明確地轉換爲浮動,使用float,這樣float(input("question:"))

試試這個:

def main(): 
    weight = 0.0 
    weight = float(input("Enter the weight of your package: ")) 
    #call module to calculate and display shipping charges 
    calcAndDisplayShipping (weight) 

#module for calculating and displaying shipping charge 
def calcAndDisplayShipping (wt): 

    #named constants for rates 
    underTwo = 1.10 
    twoToSix = 2.20, 
    sixToTen = 3.70 
    overTen= 3.80 

    #Local Variable 
    shipping = 0.0 

    #calculate charges 
    if wt > 10.0: 
     shipping = wt * overTen 
    elif wt > 6.0: 
     shipping = wt * sixToTen 
    elif wt > 2.0: 
     shipping = wt * twoToSix 
    else: 
     shipping = wt * underTwo 
    #display shipping charge 
    print ("Shipping charge for this package is: $", shipping) 

#return to main 
main() 
0

你的問題是主要是縮進,你的變量wt必須在你的函數的縮進裏面,並且你必須把輸入轉換成浮點變量,就像下面的代碼一樣,我也刪除了一個逗號放錯位置。並且請注意,最後的main()調用函數main,但不返回任何內容。

#main module 
def main(): 

    #local variables 
    weight = 0.0 

    #get package weight 
    weight = float(input("Enter the weight of your package: ")) 

    #call module to calculate and display shipping charges 
    calcAndDisplayShipping (weight) 

#module for calculating and displaying shipping charge 
def calcAndDisplayShipping (wt): 

    #named constants for rates 
    underTwo = 1.10 
    twoToSix = 2.20 
    sixToTen = 3.70 
    overTen = 3.80 

    #Local Variable 
    shipping = 0.0 

    #calculate charges 
    if wt > 10.0: 
     shipping = wt * overTen 
    elif wt > 6.0: 
     shipping = wt * sixToTen 
    elif wt > 2.0: 
     shipping = wt * twoToSix 
    else: 
     shipping = wt * underTwo 


    #display shipping charge 
    print ("Shipping charge for this package is: $", shipping) 

# calls main function!!!!! 
main()