2017-03-05 48 views
-1
##---Initializing Variable----------------------------------------------------------------------# 
#-----------------------------------------------------------------------------------------------# 
objectMass=0 
weight=0 

##---Introductory Statement: Welcome to the Program---------------------------------------------# 
#-----------------------------------------------------------------------------------------------# 
def intro(): 
    print("\n".join(["---------------------------------------------------------", 
       "Hello and Welcome to Mass to Weight Calculator", 
       "Today is a good day!", 
       "---------------------------------------------------------"])) 

##---The getMass module gets user input for mass & stores in getMass reference modules----------# 
#-----------------------------------------------------------------------------------------------# 
def getMass(): 
    objectMass=float(input("Please enter the Mass of the object you want calculated: ")) 
    return objectMass 

##--The weightConversion module calculates inputted mass into weight----------------------------# 
#-----------------------------------------------------------------------------------------------# 
def weightConversion(objectMass): 
    gravity = 9.8 
    weight=float(objectMass)*gravity 
    return weight 

##--Calculation Module compares weight of object and prints out sentence associated to weight---# 
#-----------------------------------------------------------------------------------------------# 
def massToWeight(objectMass, weight): 
    if weight > 1000: 
     print("\n".join(["Holy Smokes, your weight(in newtons) is:", format(weight, ".2f"), 
         "It's way too heavy!!!"])) 

    if weight < 10: 
     print("Aww man, I can pick this up with one finger.", "It's way too light!", 
         "This weight(in newtons) is:", format(weight, ".2f")) 

    else: 
     print("This weight(in newtons):", format(weight, ".2f"), "is just right.") 
    return 

##---Closing Statement--------------------------------------------------------------------------# 
#-----------------------------------------------------------------------------------------------# 
def closingStatement(): 
    print("\n".join(["---------------------------------------------------------", 
       "Thank you for using our Mass to Weight Calculator!", 
       "Please use us for all your calculation needs."])) 
    return 

##---Main module--------------------------------------------------------------------------------# 
#-----------------------------------------------------------------------------------------------# 
def main(): 
    ##Introductory Statement 
    intro() 

    ##---Get Mass 
    objectMass=getMass() 

    ##Calculate Mass to Weight 
    weight=weightConversion(objectMass) 

    ##Compare results 
    massToWeight(objectMass, weight) 

    ##Closing Statement 
    closingStatement() 

main() 

嘿大家。當我的第一個條件> 1000在massToWeight模塊中遇到並且不知道爲什麼時,我的程序正在返回我的else。該程序不需要模塊,但想要練習使用Python中的模塊。返回else語句無論if條件是否滿足Python

有人可以幫忙嗎?

謝謝

達里爾

+0

你可以提供一個小例子嗎?只有在if-else的問題上有很多噪音。 –

回答

1
if weight > 1000:       #IF1 
    print() 
if weight < 10:       #IF2 
    print() 
else: 
    print() 

的問題是,IF2應該已經elif

讓我們來看一個例子:

  1. 讓體重= 10000
  2. IF1條件將通過,它將被執行。
  3. 現在,因爲IF2不是elif,所以這將被視爲完全獨立的新If-else語句。
  4. IF2條件將失敗,然後它的else部分將被執行。
0

使用

elif weight < 10: 

,而不是

if weight < 10: