Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combinations
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,281 Sep-24-2020, 02:26 PM
Last Post: buran
  Four digit combinations EHod 4 7,700 Aug-13-2017, 09:14 PM
Last Post: EHod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020