random integers to have speficic value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adent
    New Member
    • Apr 2014
    • 4

    random integers to have speficic value

    hye, i,m new in programming.
    i want to random
    x = 1234

    the function should chose any number from x
    for example
    random.function (x) = 3

    is there a way
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Is this what you mean?
    Code:
    >>> import random
    >>> random.choice([1,2,3,4,5,6,7,8,9,0])
    2
    >>>

    Comment

    • adent
      New Member
      • Apr 2014
      • 4

      #3
      no it bit like this
      Code:
      {x = int(raw_input('Enter a four digit number:'))
      
      y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
      
      if (y == "1"):
          su = randint(x()) +  randint(x())
          print (su)}
      Last edited by Rabbit; Apr 30 '14, 01:07 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      • anmoluppal
        New Member
        • Nov 2013
        • 7

        #4
        Yeah, surely there is a way out .... so the idea is same .. we have to choose
        Code:
        random.choice(<list name>)
        As we know that int are not iterable so we first convert the input number into a string type and then call the
        Code:
        random.choice()
        function .... in ur case we can do it as :

        Code:
        import random
        random_choice=random.choice(str(x))

        Comment

        • adent
          New Member
          • Apr 2014
          • 4

          #5
          <Enter a four digit number:1234>
          <choose operation (1)Addition (2)Subtraction (3)Multiplicati on (4)Division>
          < 2>
          Traceback (most recent call last):
          File "C:\Users\-\main.py", line 18, in <module>
          su = random_choice - random_choice
          TypeError: unsupported operand type(s) for -: 'str' and 'str'

          hi, i have follow ur code. but i get this error.
          Code:
          from random import randint
          import sys
          import random
          
          
          x = int(raw_input('Enter a four digit number:'))
          random_choice = random.choice(str(x))
          
          su=0
          
          #write down your logic here
          y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
          
          if (y == "1"):
              su = random_choice +  random_choice
              print (su)
          elif (y == "2"):
              su = random_choice -  random_choice
              print (su)
          elif (y == "3"):
              su = random_choice *  random_choice
              print (su)
          elif (y == "4"):
              su = random_choice /  random_choice
              print (su)
          else:
              print ("NO")
          
          print("New number is: %d" %su )

          Comment

          • anmoluppal
            New Member
            • Nov 2013
            • 7

            #6
            this is probably because
            Code:
            random_choice = random.choice(str(x))
            is returning a "string" value since the input argument is of type string .... but to perform arithmetic operations, you will need to convert that back into int so the above line should look like :
            Code:
            random_choice = int(random.choice(str(x)))
            and your overall code should go like this :
            Code:
            from random import randint
            import sys
            import random
             
             
            x = int(raw_input('Enter a four digit number:'))
            [B]random_choice = int(random.choice(str(x)))[/B]
             
            su=0
             
            #write down your logic here
            y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
             
            if (y == "1"):
                su = random_choice +  random_choice
                print (su)
            elif (y == "2"):
                su = random_choice -  random_choice
                print (su)
            elif (y == "3"):
                su = random_choice *  random_choice
                print (su)
            elif (y == "4"):
                su = random_choice /  random_choice
                print (su)
            else:
                print ("NO")
             
            print("New number is: %d" %su )
            I tried it on my machine and it worked fine :) I hope it worked for u as well

            Comment

            • adent
              New Member
              • Apr 2014
              • 4

              #7
              thank you, it worked

              Comment

              Working...