【问题标题】:Python While loop (check line 6)Python While 循环(检查第 6 行)
【发布时间】:2016-03-07 11:49:45
【问题描述】:
def purchace():
    total=0
    while keepgoing == ("y" or "Y"):

    item= float(input("Enter price of the item")
    if (item<=100):
        dis=(.15*item)
        price=(item-dis)
    else:
        dis=(.25*item)
        price=(item-dis)
    total+= item
    keepgoing = raw_input("Do you want to add more items? (y/n)")

print("Your total is ",total)                    
print("Thank you")

def main():
    purchase()
main()

第 6 行“if (item

【问题讨论】:

  • 您的while 条件也是错误的。 while keepgoing in ("y", "Y"):.

标签: python while-loop boolean do-while


【解决方案1】:

您缺少上一行的右括号:

item = float(input("Enter price of the item"))

【讨论】:

  • 另外,请注意,您在此代码中使用了 很多 不必要的括号。简单的赋值和 if 表达式不需要它们。
  • 真的吗?我检查了至少 3 次...谢谢!!
  • 注明。然而,教授就是这样教我们的
  • 你需要在循环之前初始化keepgoing。在循环的底部,我认为您想将“价格”添加到总计中,而不是“项目”。
  • 如果你以后要进入专业编程领域,你需要忘记你在这里做的一些事情。查看典型 Python 编码风格的“PEP-8”标准。
【解决方案2】:
def purchase():
total=0
keepgoing = "y"
while keepgoing == ("y" or "Y"):

    item= float(input("Enter price of the item: "))
    if (item<=100):
        dis=(.15*item)
        price=(item-dis)
    else:
        dis=(.25*item)
        price=(item-dis)
    total+= price
    keepgoing = raw_input("Do you want to add more items? (y/n)")

print("Your total is ",total)                    
print("Thank you")

def main():
    purchase()
main()

这个运行完美,在原版上我也错过了一些其他的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 2021-09-09
    • 2013-01-23
    • 1970-01-01
    • 2017-11-09
    • 2017-04-03
    • 2015-12-15
    • 2016-02-13
    相关资源
    最近更新 更多