Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

How to apply recursion in python program

am currently working on a python program that ships boxes of books based on order information given by the user. I have two boxes large and small box am wholly concentrating on the height dimension of the box or stack. Mathematically am able to think and generate the required formulas but for this program but putting it in a code, my brain kind of locks out at some step whether to apply recursion or not.

Firstly, prompt user for input

# Box shipping

# Height in inches
big_box_height = int(input('Please enter the hieght of Large box: '))

# Height in inches
small_box_height = int(input('Please enter the hieght of Small box: '))

# Height/thickness in inches
book_thickness = int(input('Please enter the thickness of your books: '))

# Number of books
num_of_books = int(input('Please enetr the numbers of books to order: '))

secondly, use the user input to evaluate mathematically, to get total number of books to fill large box, the program should try all means to fill out the large boxes and not partially.

I have created a function for this case

# Fill large box.

def large_box(big_box_height, book_thickness, num_of_books):

    # Get total number of books to fill the large box
    total_num_of_books_to_fill_big_box = big_box_height % book_thickness

    # Check users input, number of books given equal number of books to fill large book
    if num_of_books == total_num_of_books_to_fill_big_box:
        print('Shipping 1 box
 1 large')

The next part is to add else statement to evaluate if number of books given is larger than the required books to fill large box. I have created an empty list to try to append total number of large boxes to be filled based on the user’s data input evaluation.

  elif num_of_books > total_num_of_books_to_fill_big_box:
        big_boxes = []

Mathematical description. I have 2 boxes of the same width and base, only height differs based on user input. sample data. if user input is height of large box = 20 inches height of small box = 10 inches box thickness is = 2 inches number of books is = 10 books By simple cross multiplication, I know that 10 books of 2 inches will fill large box at once then ship it or print shipping 1 large box.

Now if the numbers of books the user inputs is 30books, this will fill 3 large boxes, that is when the second else statement comes in handy, now my question is how do I need to apply recursion for large box function or what should I do. This as well applies for small box, provided I do not ship partially fill box. First attempt to fill is large box. I understand that recursion can help iterate through total number of books until I have zero books left. But the way to implement it, am stuck. Kindly help.

question from:https://stackoverflow.com/questions/66053488/how-to-apply-recursion-in-python-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...