繁体   English   中英

我的程序一直循环,我不知道为什么

[英]my program keeps looping and I don't know why

用户需要输入包裹重量,在我输入包裹后,它给了我正确的答案,但答案不断重复。

继承人的代码:

more = "y"

count = 0

total = 0
x = 0


while(more == "y"):
    lbs = eval(input("Please enter the weight of the package: "))

    if (lbs >= 1 and lbs <= 2):
        op1:(lbs * 1.10)
        x = op1
        count += 1
        total += x
        print("The current package shipping cost is:",op1)

    if (lbs > 2 and lbs <= 6):
        op2 = (lbs * 2.20)
        x = op2
        count += 1
        total += x
        print("The current package shipping cost is:",op2)

    if (lbs > 6 and lbs <= 10):
        op3 =(lbs * 3.70)
        x = op3
        count += 1
        total += x
        print("The current package shipping cost is:",op3)


    if (lbs > 10):
        op4 =(lbs * 3.80)
        x = op4
        count += 1
        total += x
        print("The current package shipping cost is:",op4)

    if (lbs == 0):
        print("your package cannot be delivered")

    more = input ("Do you want to enter another Package? <y/n>: ")
while(more == "n"):
    print("Your total shipping cost is:",total,"for",count,"packages")

   

你为什么不从

while True:

并在每个if评估后放置一个break ,然后您只会得到正确的输入,如果输入不正确,while 循环将再次从输入问题开始。 输入问题还需要重量需要以磅为单位的信息。

这一行——

while(more == "n"):
     print("Your total shipping cost is:",total,"for",count,"packages")

您不需要 while 循环。 做就是了 -

print("Your total shipping cost is:",total,"for",count,"packages")

此外,您不需要eval() ,只需使用int() -

lbs = int(input("Please enter the weight of the package: "))

您可以使用range()函数而不是 >= <= ... 等。 例如——

if lbs in range(1,2+1): # Not really useful, still you could use this
    # code

它并不重要,因此为简单起见,您不需要使用range函数

由于您删除了有关您的代码的先前问题,因此给您带来了错误并且功能无法正常工作......在这里我将解释您的错误和新程序是什么。

1.您的第一个错误是您使用了def greeting函数并且您没有完成该函数并且您必须定义调用该函数时该函数应该做什么。 您使用了def Greeting():但您没有完成它,如果您使用的是def函数,您总是必须在 () 之后完成它:

2.你的第二个错误是你的while循环不正确。它有一些逻辑错误

3.你的第三个错误是你的函数(op1.op2,op3,op4)格式错误,这就是为什么你的程序没有像你预期的那样工作

🎁在这里,我更新了您在 2021 年 10 月 22 日提出的上一个问题......请仔细阅读此代码并将其与您的代码进行比较并了解您的错误。

祝你好运,总是从你的错误中吸取教训:)

新代码:

def Greeting():
    print('good morning')
Greeting()
n=str(input("please input full name: "))
print("hello",n,"this program will allow you to input package weight and display price")


package_count=0 
total=0  
more=False
while more==False:
    more = input ("Do you want to enter another Package? <y/n>: ")
    if(more == "y"):
        lbs = int(input("Please enter the weight of the package: "))
    else:
        print('your total shipping cost is:',total,"for",package_count,'packages')
        break
if (lbs>=1 and lbs<=2):
        op1=(lbs*1.10)
        print('The current package shipping cost is: ',op1)
        package_count=package_count+1
        total=op1

elif(lbs>2 and lbs<=6):
    op2 = (lbs * 2.20)
    print('The current package shipping cost is:',op2)
    package_count=package_count+1
    total=total+op2

elif(lbs > 6 and lbs <= 10):
    op3 =(lbs * 3.70)
    print("The current package shipping cost is:",op3)
    package_count=package_count+1
    total=total+op3

elif(lbs>10):
    op4=(lbs*3.80)
    print("The current package shipping cost is:",op4)
    package_count=package_count+1
    total=total+op4


else:
    print("your package cannot be delivered")
more=False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM