Reputation: 155
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]
Upvotes: 0
Views: 785
Reputation: 30200
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()
Upvotes: 2