https://app.dataquest.io/m/1010/conditional-statements-practice-problems/16/finding-the-pair
Hi,
In this question, why is the answer not include other results like 39 and 61?
https://app.dataquest.io/m/1010/conditional-statements-practice-problems/16/finding-the-pair
Hi,
In this question, why is the answer not include other results like 39 and 61?
As we are looping through the end it will always give last combination. If you print value1
and value2
you can see several possibility
for x in values:
for y in values:
if x + y == 100 and x != y:
value1 = x
value2 = y
print(value1, value2)
Output
85 15
85 15
39 61
61 39
15 85
15 85
Thanks for the answer Dishin.
Glad it helps! Please consider to like post or mark it as a solution if you found helpful.
GUIDELINE #2: Accept and mark answer as
Solution
If you find a reply that answers your question satisfactorily, please mark it as
Solution
. Doing so will help -
- Others learners, who are searching for the same problem, find the solution faster
- With the Learning Assistant program - by marking the answer as solution, you can directly help the person who helped you.
after sorting it out, I’ve made a solution that lists all the pairs:
empty_list = []
for i in values:
for c in values:
if i + c == 100 and i != c:
empty_list.append([i, c])
Hi! For me it happened the same, i recalled only one pair of numbers because i wrote the print code outside the loop. If you print inside the loop it shows you all the combinations. Could you explain why? thank you!
Also, in the beginning i started with value1= and value2= instead of value1=None and value2=None, is this correct? the answer seems to be ok but i need to know if my solution was ok regarding to the syntax. Thanks thanks thanks!
Hi! For me it happened the same, i recalled only one pair of numbers because i wrote the print code outside the loop. If you print inside the loop it shows you all the combinations.
Hi there!
I noticed that the solution initialises value1 and value2 as None
. Is this not required?
Many thanks,
Andrea
This is not required. You can code without initializing the variables as None
and get the result.
values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 10, 62, 48, 32, 96, 75, 15, 50, 50]
for x in values:
for y in values:
if x + y == 100 and x != y:
value1 = x
value2 = y
print(value1, value2)