Python Forum
Combinations - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Combinations (/thread-14782.html)



Combinations - mnnewick - Dec-17-2018

Here is my code:
#Task 3 Theif#
import itertools
n1 = input("Enter your 1st known number? ")
    

n2 = input("Enter your 2nd known number? ")


n3 = input("Enter your 3rd known number? ")


n4 = input("Enter your 4th known number? ")

numbers = (n1,n2,n3,n4)

verify = ()
while verify != "y" or verify != "n":
  print(n1, n2, n3, n4)
  verify = input(int("Is this correct? (y/n)")
if verify == "y":
    set(list(itertools.permutations([1, 2, 2, 4])))
Task:
Design and write a program that displays all the possible combinations for any four numerical digits entered by the user. The program should avoid displaying the same combination more than once.

PROBLEM:
The code above is what I have got but it keeps giving me errors. Can someone point out where I am wrong and correct it please, I have been stuck on this for several days now.

If I am off track and doing it wrong,can someone send an answer so I can see where I went wrong as I have no clue


RE: Combinations - ichabod801 - Dec-17-2018

If you are printing combinations (no repeats), you should be using itertools.combinations, not itertools.permutations (which prints repeats of the same numbers, just in different orders). The task is poorly specified. It does not say what size of combination. I would check with the professor, but probably they want all sizes. So you will have to loop through the sizes (where you have the one size on line 20).

I would put all of this in a big while True loop. That way, if verify == 'n', it can loop back and ask them again. You would just need to add a break to the if verify == 'y' block.