0

Right now I am trying to make a simple program to separate the javascript links of a website but I'm running into issues with a while loop.

Here would be an example of an input:

001_usa_wool.jpg
002_china_silk.jpg
003_canada_cotton.jpg
004_france_wool.jpg
done

A simplified version of my code with just 3 parts is the following:

def ParseData(input):
    data = input.split('_')

    d = {}
    d['sku'] = data[0]
    d['country'] = data[1].capitalize()
    d['material'] = data[2].capitalize()
    return d

def Sku():
    myData = ParseData(input)
    sku = myData['sku']
    return sku

def Country():
    myData = ParseData(input)
    country = myData['country']
    return country

def Material():
    myData = ParseData(input)
    material = myData['material']
    return material

def Output():
    print (Sku()+'\t'+
           Country()+'\t'+
           Material()+'\t'+
           '\n')

Now here is how I tried to read it line by line:

def CleanInput(input):
    clean = input.split('.jpg')
    count = 0
    while (clean[count] != 'done'):
        ParseData(clean[count])
        Output()
        count = count+1

input = input('Enter your data: ')
CleanInput(input) 

I believe I am not implementing the while loop correcting since my output is similar to:

001   Usa   Wool
001   Usa   Wool
001   Usa   Wool
4
  • try iterating on the items in clean: for elem in clean: ... Commented Jun 19, 2015 at 20:01
  • How are you giving the input to the program? Are you giving complete input you stated in the start of the question as whole to the input() function? Commented Jun 19, 2015 at 20:02
  • Yes, I am pasting 001_usa_wool.jpg002_china_silk.jpg003_canada_cotton.jpg004_france_wool.jpgdone as the entire input Commented Jun 19, 2015 at 20:05
  • Your code has some masking (e.g. input) and severe repetition problems. Commented Jun 19, 2015 at 20:13

3 Answers 3

1

The issue is not exactly in your while loop , but in your functions - Output() , Sku() , Material() and Country() .

In the function Output() , you are printing the values by directly calling Sku(), etc.

In each of the function, I will take one as example - Sku() , you are calling parseData on input (Though this is a very bad naming, please use a better name, with this name you are overwriting the built-in input function, and later on you cannot call input() to take inputs from user)

The input always contains the entire string you inputted , and hence it contains all the .jpg names, when parseData goes through it, it always only picks up the first one.

Instead of using input in each function, we should make the functions parameterized and send in the value that needs to be printed as a parameter, as you are doing for parseData . And Example code -

def Sku(toprint):
    myData = ParseData(toprint)
    sku = myData['sku']
    return sku
.
.
.
def Output(toprint):
    print (Sku(toprint)+'\t'+
           Country(toprint)+'\t'+
           Material(toprint)+'\t'+
           '\n')

And in the while loop send in the current value to print as parameter to Output() -

def CleanInput(input):
    clean = input.split('.jpg')
    count = 0
    while (clean[count] != 'done'):
        ParseData(clean[count])
        Output(clean[count])
        count = count+1

Also , please do not use input as the name of the variable , it can cause issues , as i previously stated , as you are overwriting the built-in input function with that.

Sign up to request clarification or add additional context in comments.

Comments

1

Personally, I would make it more pythonic:

def CleanInput(input):
    clean = input.split('.jpg')
    for count, elem in enumerate(clean):
        if elem == 'done':
            break
        ParseData(elem)
        Output()
    return count

input_data = input('Enter your data: ')
how_many = CleanInput(input_data)

Assuming you really need count. By the way: you aren't using the return value of ParseData

Comments

0

You have too many functions that call each other and take on vague requirements. It's hard to see what returns something, what prints, and so on. For example, your CleanInput calls ParseData and Output, but Output calls Sku, Country, and Material, each of which also calls ParseData. Oh, and capitalized variables should be reserved for classes - use snake_case for functions.

>>> s = "001_usa_wool.jpg002_china_silk.jpg003_canada_cotton.jpg004_france_wool.jpgdone"
>>> print(*("{}\t{}\t{}".format(*map(str.capitalize, item.split('_')))
... for item in s.split('.jpg') if item != 'done'), sep='\n')
001     Usa     Wool
002     China   Silk
003     Canada  Cotton
004     France  Wool

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.