问题
I'm doing my first programming course and I'm stuck already.
I'm trying to make a program that will tell you how many toy's you can make given how many pieces the user has.
To create the toy, you need to have 5 upper pieces and 2 lower pieces.
I've assigned the upper pieces to the letter 'a' and lower pieces to the letter 'b'
This is what I currently have done
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?")
So for example, if you input you have 7 upper pieces and 5 lower pieces, it should tell you you are able to create 1 toy and that you would have 2 upper pieces left and 3 lower pieces left.
回答1:
u, l = input('upper? '), input('lower? ')
nToys = min( int(u)/5, int(l)/2 )
upperLeft = u - nToys*5
lowerLeft = l - nToys*2
回答2:
okey dokey. I take it you're using an older version of python because of the way you're using print. Here's how I'd do it:
a = input()
b= input("How many lower pieces did you bring?")
upper_left_over = a % 5
lower_left_over = b % 2
upper = int(int(a)/5)
lower = int(int(b)/2)
if upper > lower:
toys = lower
print("You can create", str(toys), "toys")
else:
toys = upper
print("You can create", str(toys), "toys")
print(upper_left_over, lower_left_over)
This is obviously messy. You can clean it up. Hope this helps!
来源:https://stackoverflow.com/questions/25880441/troubles-finishing-first-assignment-python