用户需要输入包裹重量,在我输入包裹后,它会给我正确的答案,但答案不断重复。
代码如下:
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")发布于 2021-09-24 03:06:27
这一行-
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函数
发布于 2021-09-24 03:00:24
你为什么不直接从
while True:并在每个if求值后放置一个break,那么您只会得到正确的输入,如果输入不正确,while循环将再次从输入问题开始。输入问题还需要权重需要为磅的信息。
发布于 2021-10-23 03:03:34
既然你删除了之前关于你的代码给你的错误和函数不是working...here的问题,我将解释你的错误和新程序。
1.您的第一个错误是您使用了def greeting函数,并且您没有完成该函数,并且您必须在调用函数时定义该函数应该执行的操作。您使用了def Greeting():,但没有完成它,如果您使用的是def函数,则必须在()之后完成它:
2.第二个错误是while循环不是correct.It有一些逻辑错误
3.你的第三个错误是你的函数(op1.op2,op3,op4)的格式是错误的,这就是为什么你的程序不能像你期望的那样工作
在这里,我更新了你在10月22日提出的前一个问题,2021...please,检查一下这段代码,并将它与你的代码进行比较,了解你的错误。
祝你好运,朋友,永远从错误中吸取教训:)
新代码:
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=Falsehttps://stackoverflow.com/questions/69308976
复制相似问题