【问题标题】:Returning else statement regardless if If conditions met in Python [duplicate]无论 Python 中是否满足 If 条件,都返回 else 语句 [重复]
【发布时间】:2017-03-05 06:58:56
【问题描述】:
##---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()

大家好。当我在 massToWeight 模块中满足 > 1000 的第一个条件并且不知道为什么时,我的程序正在返回我的 else。该程序不需要模块,但想练习在 python 中使用模块。

有人可以帮忙吗?

谢谢你,

达里尔

【问题讨论】:

  • 你能提供一个更小的例子吗?仅针对 if-else 的问题就会产生大量噪音。

标签: python if-statement


【解决方案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 部分将被执行。

【讨论】:

    【解决方案2】:

    使用

    elif weight < 10:
    

    而不是

    if weight < 10:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 2012-12-17
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      相关资源
      最近更新 更多