这件事让我抓狂。在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()
如果“display”的意思是“print”,那么程序不打印行李类型的原因很简单:没有以行李类型作为参数调用print函数的实例。退货不会导致打印
顺便说一句,
value == "plastic" or value == "Plastic" or value == "paper" or value == "Paper"
可以替换为value.lower() in ['plastic','paper']
相关问题 更多 >
编程相关推荐