麻烦整理Python项目[v2]

时间:2014-09-17 02:48:24

标签: arc4random

print "Welcome to Amanda's Toy Factory"

print "At this factory, you will need to have 5 upper pieces and 2 lower pieces to create  a toy"

x = input("How many toys would you like to make?")

print "To create",x,"toys, you will need", x*5, "upper pieces and", x*2, "lower pieces"

a = input("How many upper pieces did you bring?")
b = input("How many lower pieces did you bring?")

例如:如果您输入了23件U件和5件L件,它应该告诉您可以制作2件玩具,剩下13件U件和1件。

编辑)谢谢Larry告诉我出了什么问题,我纠正并提交了。

2 个答案:

答案 0 :(得分:3)

你没有Python问题,你遇到了算法问题。

为了弄清楚可以建造的玩具的最大数量,你需要问自己“U件可以制造多少玩具”和“L件可以制造多少玩具”?你可以建造的玩具的实际数量是这两个数字中的较低者,对吧? (因为当你用完一个或其他部件时你必须停止制造玩具。)

你如何计算11件,25件或32件中可以制作多少件5件式玩具?您可以根据每件玩具所需的件数来划分可用件数。

你似乎已经触及了这个想法,因为你要把所需的碎片分开 - 但是(从你的变量的名称)你似乎相信这会计算出使用的碎片数量,而不是玩具的数量可以建造。而且你没有找到两个玩具数字中的较低者。

一旦你解决了你可以建造的玩具数量,就可以很容易地计算掉剩下的部分。

答案 1 :(得分:0)

正如Larry所说,在使用任何语言编写解决方案之前,您必须先检查解决问题的方法。

在此之前阅读last ask后,我相信您的期望是这样的。

toys = input('How many toys would you like to build?')

toy = {'upper': 5, 'lower': 2}

print 'To create {toys} you will need {upper} upper pieces and {lower} lower pieces'.format(toys=toys, upper=toy['upper']*toys, lower=toy['lower']*toys)

stock = {
    'upper': input('How many U Pieces did you bring?'),
    'lower': input('How many L Pieces did you bring?'),
}

complete = {
    'upper': stock['upper'] / toy['upper'],
    'lower': stock['lower'] / toy['lower'],
    }

left_parts = {
    'upper': (toys * toy['upper']) % stock['upper'],
    'lower': (toys * toy['lower']) % stock['lower'],
    }

if toys * toy['upper'] >= stock['upper'] and toys * toy['lower'] >= stock['lower']:
    print 'You can build {toys} complete toys and no pieces will left in your stock.'.format(toys=toys)
else:
    print 'You can build {toys} complete toys and {lower} lower pieces and {upper} upper pieces will left in your stock.'.format(toys=min(complete.values()), lower=left_parts['lower'], upper=left_parts['upper'])