0

in the code below the user is asked to input items. However the first input by the user is being ignored. For this I cannot figure out why. Any ideas?

def stupidFacebookPost():
    interger = int((input("Enter a Interger Value")))
    Product = int((input("Enter a Product Value")))
    intergerValues = []
    productValues = []
    commonValue = []
    while interger != ''  and Product != '':
        try:
            interger = int((input("Enter a Interger Value")))
            intergerValues.append(interger)
            print(intergerValues) #testing
            Product = int((input("Enter a Product Value")))
            productValues.append(Product)
            print(productValues) #testing
        except ValueError:
            break


    for intergers in  intergerValues:
        for products in productValues:
            commonValue.append(int(products) // int(intergers))
            print(commonValue) #test
            intergerValues.pop([0])
            productValues.pop([0])
    print('the Common Value is {}'.format((commonValue)))
    #testInterger = input("Enter a test value Interger")

output just for test

stupidFacebookPost()
Enter a Interger Value1
Enter a Product Value1
Enter a Interger Value2
[2]
Enter a Product Value4
[4]
Enter a Interger Value3
[2, 3]
Enter a Product Value6
[4, 6]

1 Answer 1

2

The results from lines 2 and 3 are never appended to the corresponding lists.

So you could just initialize the lists with those values:

interger = int((input("Enter a Interger Value")))
Product = int((input("Enter a Product Value")))
intergerValues = [interger,]
productValues = [Product,]
#...

But this isn't the best solution. It forces us to duplicate code (the input lines).

Also, the construction of the loop is a bit off since the values are appended to the list regardless of their contents, and we'll never break out since the input is immediately converted to integers (so integer != '' will always be true)

What we "really want", in this case, is a do-while loop. Do something (prompt the user for input), while some condition is met (the input is not empty strings).

In python, we don't have do-while loops but we can accomplish the same functionality (and more) with an infinite loop + break statement.

So a do while loop that may be written like:

do:
    do_something1
    do_something2
while(<some condition>)

can be written in python as:

while True:  # Infinite loop
    do_something1
    do_something2
    if not(<some condition>): break

Note we inverted the logic at the condition. In order to continue looping in a do/while loop, we want the condition to be True, in the while/break construct, the condition must be False.

Consider a slightly different structure:

def stupidFacebookPost():
    integerValues = []
    productValues = []
    commonValue = []
    while True:
        try:
            integer = input("Enter a Integer Value ")
            if integer == '': break
            Product = input("Enter a Product Value ")
            if Product == '': break
            integerValues.append(int(integer))
            productValues.append(int(Product))
            print(integerValues) #testing
            print(productValues) #testing
        except ValueError:
            break

    for integers in  integerValues:
        for products in productValues:
            commonValue.append(int(products) // int(integers))
            print(commonValue) #test
            integerValues.pop([0])
            productValues.pop([0])
    print('the Common Value is {}'.format((commonValue)))

stupidFacebookPost()

And if you're using Python 2 -- your input functions should be raw_input.

Note, it looks like you still have issues with the logic in your second loop, but at least you get there with reliable results now.

Edit The above code was written to illustrate how you could get do/while functionality in python. Notably, with if <condition>: break. But since you've already (correctly) wrapped the code in a try/except block, you could remove the explicit breaks and rely on the except block to break you out. For example, something like:

def stupidFacebookPost():
    integerValues = []
    productValues = []
    commonValue = []
    while True:
        try:
            integer = int(input("Enter a Integer Value "))
            Product = int(input("Enter a Product Value "))
            integerValues.append(integer)
            productValues.append(Product)
            print(integerValues) #testing
            print(productValues) #testing
        except ValueError:
            break

    for integers in  integerValues:
        for products in productValues:
            commonValue.append(int(products) // int(integers))
            print(commonValue) #test
            integerValues.pop([0])
            productValues.pop([0])
    print('the Common Value is {}'.format((commonValue)))

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

6 Comments

this is a very very impressive answer and I want to thank you very much. I spent some time looking at your code and comparing mine to it to understand the difference and it all makes sense. Additionally the conversational text you added helped a lot. I know my second loops still have issues but I am enjoying trying to figure it out. If I may is it possible that you elaborate just a bit more on why the first inputs were ignored? Because that is still fuzzy in that the code other than the while true loop is very similar. I guess I still dont understand the reason as to why the first input failed
Sure, if we number the lines of your original program starting at def... being 1, on lines 2 and 3 you get user input and set integer and Product respectively. Fine, good so far. On line 7 you're "technically" using them, but only to see whether you should loop or not, so this doesn't really count -- you're not preserving their values on this line. The next time you use integer is on line 9 where you reset it's value by assigning user input to it. It's not until like 10 where you add it to the list to preserve it. The same goes for Product... (cont'd)
You get it initially on line 3, then don't use it again until line 12 where, again, you assign something to it. The original value is then lost. It's not until line 13 where you append its value to the list, preserving it. That's why, in the first code block of my answer I suggested you might initialize the list variables with those values, effectively preserving them immediately, so that when you re-assigned to them on lines 9 and 12, the values wouldn't be lost.
again great answer and this makes sense now. Your saying that my first input is used initially just see if i satisfy the while loop, because I call input again without doing anything with the first input it is lost. I can clearly see how your code still checks for empty strings yet captures all inputs. my final question to you is this then. Does that mean that the conditions of my while loop in the old code are no longer being checked since the first non used input satisfied the condition?
Exactly right with your summary. As for your question, if I understand it correctly, the answer is no, that's not what it means. A while loop will check the condition before entering the first time, as well as every time the indented code finishes. If it's False before entering, the entire block is skipped. If it's True before entering, it's entered, the code block is run and then the condition's re-checked before it's entered again/loops. So your conditions will still be checked after the first loop iteration, just with the new values you assigned inside the loop. Does that answer it?
|

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.