Troubles Finishing First Assignment - Python [closed]

与世无争的帅哥 提交于 2019-12-23 05:34:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!