我正在摸着这个。在get_paper_or_plastic
功能中的“请选择纸张或塑料”中提供“纸张”或“塑料”后,我无法弄清楚为什么我的程序没有显示行李类型。这是我的代码:
maximum_number_of_groceries = 500
def get_string(prompt):
value = ""
value = input(prompt)
return value
def valid_real(value):
try:
float(value)
return True
except:
return False
def get_real(prompt):
value = ""
value = input(prompt)
while not valid_real(value):
print(value, "is not a number. Please provide a number.")
value = input(prompt)
return float(value)
def get_paper_or_plastic(prompt):
value = ""
value = input(prompt)
if value == "plastic" or value == "Plastic" or value == "paper" or value == "Paper":
return value
else:
print("That is not a valid bag type. Please choose paper or plastic")
value = input(prompt)
def y_or_n(prompt):
value = ""
value = input(prompt)
while True:
if value == "Y" or value == "y":
return False
elif value == "N" or value == "n":
return True
else:
print("Not a valid input. Please type Y or N")
value = input(prompt)
def get_groceries(grocery_name, quantity,paper_or_plastic):
done = False
counter = 0
while not done:
grocery_name[counter] = get_string("What grocery do you need today? ")
quantity[counter] = get_real("How much of that item do you need today?")
counter = counter + 1
done = y_or_n("Do you need anymore groceries (Y/N)?")
paper_or_plastic = get_paper_or_plastic("Do you want your groceries bagged in paper or plastic bags today?")
return counter
def calculate_total_groceries(quantity):
counter = 0
total_quantity = 0
while counter < len(quantity):
total_quantity = total_quantity + int(quantity[counter])
counter = counter + 1
return total_quantity
def grocery_list():
grocery_name = ["" for x in range (maximum_number_of_groceries)]
quantity = [0.0 for x in range (maximum_number_of_groceries)]
total_quantity = 0
paper_or_plastic = ""
get_groceries(grocery_name, quantity, paper_or_plastic)
total_quantity = calculate_total_groceries(quantity)
print ("Total number of groceries purchased is: ", total_quantity," and you have chosen a bag type of ", paper_or_plastic)
grocery_list()
答案 0 :(得分:1)
如果通过&#34;显示&#34;,你的意思是&#34;打印&#34;,那么你的程序没有打印包类型的原因很简单:你没有调用打印功能的实例袋类型作为参数。退回某些东西并不会导致它被打印出来。
BTW,value == "plastic" or value == "Plastic" or value == "paper" or value == "Paper"
可以替换为value.lower() in ['plastic','paper']