1 Replies - 185 Views - Last Post: 06 July 2019 - 05:18 AM Rate Topic: -----

#1 Yerlan999   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-July 19

Sequence making code Python 3.0

Posted 06 July 2019 - 05:01 AM

This piece of code tries to find a sequnce of number {which only somprises of this numbers (1, 2, 3, 4, 5, 6, 7, 8, 9)} that would satisfy the following reqirements. The very first two numbers have to be divisable to 2, and the first three divisable to three and so on.
Every time I run this code it takes several attempts and gives back an error.


Traceback (most recent call last):
File "..\Playground\", line 19, in <module>
main_list.remove( b );
ValueError: list.remove(x): x not in list



I am attaching the code (theCode.py), so you can run and test it if you want.
Maybe this task has no any solution at all. I am not sure. What am I doing wrong?

I need some advice in figuring out what is wrong with this code/ Thanks a lot in advance!


import random


x = 0;

while x == 0 :
     
    main_list = [1, 2, 3, 4, 6, 7, 8, 9]
    e = 5;
    
    a = random.choice(main_list);
    b = random.choice(main_list);

    main_list.remove(a);
    main_list.remove( b );
    
    n_1 = str(a) + str( b )
    
    print(n_1)
    print(main_list)
    
    if (int(n_1) % 2) == 0: 
         #continuation of the code execution 
        print ("Everything is Okay ;-)  GOING ON...")
        
        c = random.choice(main_list);

        main_list.remove(c);
    
        n_2 = str(n_1) + str(c);
        
        print(n_2)
        print(main_list)
        
        if (int(n_2) % 3) == 0:
            #continuation of the code
            print ("Everything is Okay ;-)  GOING ON...")
            
            d = random.choice(main_list);
            
            main_list.remove(d);
            
            n_3 = str(n_2) + str(d);
            
            print(n_3)
            print(main_list)
            
            if (int(n_3) % 4) == 0:
                #continuation of the code
                print ("Everything is Okay ;-)  GOING ON...")
                
                n_4 = str(n_3) + str(e);
                
                print(n_4)
                print(main_list)
                
                if (int(n_4) % 5) == 0: 
                    #continuation of the code
                    print ("Everything is Okay ;-)  GOING ON...")
                    
                    f = random.choice(main_list);
            
                    main_list.remove(f);
                    
                    n_5 = str(n_4) + str(f);
                    
                    print(n_5)
                    print(main_list)
                    
                    if (int(n_5) % 6) == 0:
                        #continuation of the code
                        print ("Everything is Okay ;-)  GOING ON...")
                        
                        g = random.choice(main_list);
            
                        main_list.remove(g);
                        
                        n_6 = str(n_5) + str(g);
                        
                        print(n_6)
                        print(main_list)
                        
                        if (int(n_6) % 7) == 0: 
                            #continuation of the code
                            print ("Everything is Okay ;-)  GOING ON...")
                            
                            h = random.choice(main_list);
                
                            main_list.remove(h);
                            
                            n_7 = str(n_6) + str(h);
                            
                            print(n_7)
                            print(main_list)
                            
                            if (int(n_7) % 8) == 0: 
                                 #continuation of the code
                                print ("Everything is Okay ;-)  GOING ON...")
                                
                                i = random.choice(main_list);
                    
                                main_list.remove(i);
                                
                                n_8 = str(n_7) + str(i);
                                
                                print(n_8)
                                print(main_list)
                                
                                if (int(n_8) % 9) == 0:
                                     #finalizing part of the code
                                    print (n_8) 
                                    print ("Everything is DONE ;-) ")
                                    print ("The Code execution has FINISHED!!!")
                                    
                                    x = 1;
                                    break
                    
                    
                    
                                else:
                                    print ("Ooops, something went wrong....start it again, please! ")
                                    continue
                            else: 
                                print ("Ooops, something went wrong....start it again, please! ")
                                continue
                        else: 
                            print ("Ooops, something went wrong....start it again, please! ")
                            continue
                    else:
                        print ("Ooops, something went wrong....start it again, please! ")
                        continue
                else: 
                    print ("Ooops, something went wrong....start it again, please! ")
                    continue
            else:
                print ("Ooops, something went wrong....start it again, please! ")
                continue
        else: 
            print ("Ooops, something went wrong....start it again, please! ")
            continue
    else: 
        print("Ooops, something went wrong....start it again, please! ")
        continue

:code:/>/>/>

Attached image(s)

  • Attached Image

Attached File(s)


This post has been edited by modi123_1: 06 July 2019 - 05:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sequence making code Python 3.0

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: Sequence making code Python 3.0

Posted 06 July 2019 - 05:18 AM

Think about these lines.

011	    a = random.choice(main_list);
012	    b = random.choice(main_list);
013	 
014	    main_list.remove(a);
015	    main_list.remove( b );


What happens if a and b are the same number? You would remove it once in line 14, and then attempt to remove it again in line 15.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1