[英]my program keeps looping and I don't know why
the user is required to input package weight and after I input the packages it gives me the correct answer but the answer keeps on repeating.用户需要输入包裹重量,在我输入包裹后,它给了我正确的答案,但答案不断重复。
heres the code:继承人的代码:
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")
why don't you just start with你为什么不从
while True:
and place a break
after each if
evaluation, then you get only correct input, and in case of incorrect input the while loop starts again with the input question.并在每个
if
评估后放置一个break
,然后您只会得到正确的输入,如果输入不正确,while 循环将再次从输入问题开始。 The input question also requires the information that the weight needs to be in lbs.输入问题还需要重量需要以磅为单位的信息。
This line -这一行——
while(more == "n"):
print("Your total shipping cost is:",total,"for",count,"packages")
You do not need the while loop.您不需要 while 循环。 Just do -
做就是了 -
print("Your total shipping cost is:",total,"for",count,"packages")
Additionally, you do not need eval()
, just use int()
-此外,您不需要
eval()
,只需使用int()
-
lbs = int(input("Please enter the weight of the package: "))
You could use range()
function instead of the >= <= ... etc. things.您可以使用
range()
函数而不是 >= <= ... 等。 For eg -例如——
if lbs in range(1,2+1): # Not really useful, still you could use this
# code
It is not significant, so to keep it simple, you do not need to use the range
function它并不重要,因此为简单起见,您不需要使用
range
函数
Since you deleted your previous question about your code is giving you errors and functions are not working...here I will explain what are your errors and the new program.由于您删除了有关您的代码的先前问题,因此给您带来了错误并且功能无法正常工作......在这里我将解释您的错误和新程序是什么。
1.Your first error is you used def greeting function and you didn't complete the function and you have to define what the function should do when you call the function. 1.您的第一个错误是您使用了def greeting函数并且您没有完成该函数并且您必须定义调用该函数时该函数应该做什么。 You used
def Greeting():
but you didn't complete it,if you are using a def
function you always have to complete it after():您使用了
def Greeting():
但您没有完成它,如果您使用的是def
函数,您总是必须在 () 之后完成它:
2.Your 2nd error is your while loop is not correct.It has some logical errors 2.你的第二个错误是你的while循环不正确。它有一些逻辑错误
3.Your 3rd error is your functions(op1.op2,op3,op4) are in the wrong format that's why your program didn't work as you expect 3.你的第三个错误是你的函数(op1.op2,op3,op4)格式错误,这就是为什么你的程序没有像你预期的那样工作
🎁here i update your previous question which was asked on 22nd October 2021...please go through this code and compare it to your one and learn your errors. 🎁在这里,我更新了您在 2021 年 10 月 22 日提出的上一个问题......请仔细阅读此代码并将其与您的代码进行比较并了解您的错误。
Good Luck buddy and always learn from your errors:)祝你好运,总是从你的错误中吸取教训:)
new code:新代码:
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.