number_of_people = input("Enter number of people attending the picnic: ")
if number_of_people.isnumeric():
number_of_donuts = input("Enter number of donuts each person will want: ")
if number_of_donuts.isnumeric():
min_box = (number_of_people * number_of_donuts ) // 12
box_remainder = (number_of_people * number_of_donuts ) % 12
if box_remainder != 0:
min_box = min_box + 1
print("Minimum boxes of donuts needed: ", min_box)
donuts_left = (min_box * 12) - (number_of_people * number_of_donuts )
print("Donuts left over:", donuts_left)
if min_box <= 5:
donut_cost = 8 * min_box
elif min_box <= 20:
donut_cost = 7.50 * min_box
else:
donut_cost = 6 * min_box
print ("Donuts cost: ",format(donut_cost, '.2f'))
else:
print(number_of_donuts, "is not a valid number")
else:
print (number_of_people, "is not a valid number")
我真的很困惑。我希望能够告诉那个人他们输入了错误的输入,但是如果我将输入更改为int(输入。我不知道如果他们没有输入int就通知用户。
我收到的错误 TypeError:不能将序列乘以类型&#39; str&#39;
的非int答案 0 :(得分:0)
我认为这个问题就在这条线上了?
donuts_left = (min_box * 12) - (number_of_people * number_of_donuts )
我认为问题是number_of_people
和number_of_donuts
都是字符串,但您可能想要处理数字。
尝试以下方法。 (我添加了x = int(x)
行以将str
转换为int
s。)
number_of_people = input("Enter number of people attending the picnic: ")
if number_of_people.isnumeric():
number_of_people = int(number_of_people)
number_of_donuts = input("Enter number of donuts each person will want: ")
if number_of_donuts.isnumeric():
number_of_donuts = int(number_of_donuts)
min_box = (number_of_people * number_of_donuts ) // 12
box_remainder = (number_of_people * number_of_donuts ) % 12
if box_remainder != 0:
min_box = min_box + 1
print("Minimum boxes of donuts needed: ", min_box)
donuts_left = (min_box * 12) - (number_of_people * number_of_donuts )
print("Donuts left over:", donuts_left)
if min_box <= 5:
donut_cost = 8 * min_box
elif min_box <= 20:
donut_cost = 7.50 * min_box
else:
donut_cost = 6 * min_box
print ("Donuts cost: ",format(donut_cost, '.2f'))
else:
print(number_of_donuts, "is not a valid number")
else:
print (number_of_people, "is not a valid number")