2

In my job, I receive cartons from a manufacturer. The cartons are serially numbered and certain cartons (the first 5, those ending with 20, 50, 90 and the last 5) have to be opened and inspected. As can be seen from the code below, I think I have nailed down the issue of the first and last five cartons. I am stumped over the ones ending with 20, 50,90. The first_carton and last_carton numbers depend on the current batch of cartons.

    """
This script helps arrange cartons for inspection
How the system works:
* Lay out the first 5 cartons in the list
* Lay out the last 5 cartons in the list
* Lay out cartons ending with 20, 50, 90
"""
#Prompt the user to enter the first carton number
first_carton = int(input("First Carton: "))
#The last carton number
last_carton = int(input("Last Carton: "))

#Make the first carton number and last carton number (+1) into a range
# and make all the numbers inthe range into a list
carton_list = list(range(first_carton, last_carton+1))

#Get the first and last 5 carton numbers by slicing the carton list
firstFive = carton_list[0:5]
lastFive = carton_list[-5:]

# Function to print out numbers ending in 20, 50, and 90
def print_20_50_90_cartons():
    for i in carton_list:
        if i #ENDS WITH 20,50,90#:
            print(i)
L4c0573
  • 333
  • 1
  • 7
  • 23

2 Answers2

2

You are not calling the function and are also trying to access the carton_list from within a function. You should pass carton_list as a parameter to your function as follows:

Haven't tested this, no access to a Python interpreter right now. Doing modulo by 100 will give the remainder for dividing by a hundred (thus the last two digits)

def print_20_50_90_cartons(carton_list):
    for i in carton_list:
        last_digits = i % 100
        if last_digits in [20,50,90]:
            print(i)
Kalkran
  • 324
  • 1
  • 8
0

str has a built-in method for that: str.endswith():

def print_20_50_90_cartons():
    for i in carton_list:
        if str(i).endswith(('20', '50', '90')):
            print(i)
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
zondo
  • 19,901
  • 8
  • 44
  • 83
  • Provided that `i` is of type `str` – matino Feb 17 '16 at 11:57
  • Ha, didn't notice that it shouldn't be. Looks like GingerPlusPlus already fixed that for me. – zondo Feb 17 '16 at 12:02
  • Yes matino, i is of type int so there will be no output. However, if I understand correctly, you tried to convert i to str `_(str(i))_` right? It does not work but does not give errors either. Thanks for the insight into `_endswith_`. – Dzingai Chakazamba Feb 18 '16 at 08:51