By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

random integers to have speficic value

P: 4
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
Apr 29 '14 #1

✓ answered by anmoluppal

this is probably because
Expand|Select|Wrap|Line Numbers
  1. 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 :
Expand|Select|Wrap|Line Numbers
  1. random_choice = int(random.choice(str(x)))
and your overall code should go like this :
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2. import sys
  3. import random
  4.  
  5.  
  6. x = int(raw_input('Enter a four digit number:'))
  7. random_choice = int(random.choice(str(x)))
  8.  
  9. su=0
  10.  
  11. #write down your logic here
  12. y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
  13.  
  14. if (y == "1"):
  15.     su = random_choice +  random_choice
  16.     print (su)
  17. elif (y == "2"):
  18.     su = random_choice -  random_choice
  19.     print (su)
  20. elif (y == "3"):
  21.     su = random_choice *  random_choice
  22.     print (su)
  23. elif (y == "4"):
  24.     su = random_choice /  random_choice
  25.     print (su)
  26. else:
  27.     print ("NO")
  28.  
  29. print("New number is: %d" %su )
  30.  
  31.  
  32.  
I tried it on my machine and it worked fine :) I hope it worked for u as well

Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Is this what you mean?
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> random.choice([1,2,3,4,5,6,7,8,9,0])
  3. 2
  4. >>> 
Apr 29 '14 #2

P: 4
no it bit like this
Expand|Select|Wrap|Line Numbers
  1. {x = int(raw_input('Enter a four digit number:'))
  2.  
  3. y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
  4.  
  5. if (y == "1"):
  6.     su = randint(x()) +  randint(x())
  7.     print (su)}
Apr 30 '14 #3

anmoluppal
P: 7
Yeah, surely there is a way out .... so the idea is same .. we have to choose
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. random.choice()
function .... in ur case we can do it as :

Expand|Select|Wrap|Line Numbers
  1. import random
  2. random_choice=random.choice(str(x))
Apr 30 '14 #4

P: 4
<Enter a four digit number:1234>
<choose operation (1)Addition (2)Subtraction (3)Multiplication (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.
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2. import sys
  3. import random
  4.  
  5.  
  6. x = int(raw_input('Enter a four digit number:'))
  7. random_choice = random.choice(str(x))
  8.  
  9. su=0
  10.  
  11. #write down your logic here
  12. y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
  13.  
  14. if (y == "1"):
  15.     su = random_choice +  random_choice
  16.     print (su)
  17. elif (y == "2"):
  18.     su = random_choice -  random_choice
  19.     print (su)
  20. elif (y == "3"):
  21.     su = random_choice *  random_choice
  22.     print (su)
  23. elif (y == "4"):
  24.     su = random_choice /  random_choice
  25.     print (su)
  26. else:
  27.     print ("NO")
  28.  
  29. print("New number is: %d" %su )
  30.  
  31.  
Apr 30 '14 #5

anmoluppal
P: 7
this is probably because
Expand|Select|Wrap|Line Numbers
  1. 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 :
Expand|Select|Wrap|Line Numbers
  1. random_choice = int(random.choice(str(x)))
and your overall code should go like this :
Expand|Select|Wrap|Line Numbers
  1. from random import randint
  2. import sys
  3. import random
  4.  
  5.  
  6. x = int(raw_input('Enter a four digit number:'))
  7. random_choice = int(random.choice(str(x)))
  8.  
  9. su=0
  10.  
  11. #write down your logic here
  12. y = raw_input("choose operation (1)Addition (2)Subtraction (3)Multiplication (4)Division  ")
  13.  
  14. if (y == "1"):
  15.     su = random_choice +  random_choice
  16.     print (su)
  17. elif (y == "2"):
  18.     su = random_choice -  random_choice
  19.     print (su)
  20. elif (y == "3"):
  21.     su = random_choice *  random_choice
  22.     print (su)
  23. elif (y == "4"):
  24.     su = random_choice /  random_choice
  25.     print (su)
  26. else:
  27.     print ("NO")
  28.  
  29. print("New number is: %d" %su )
  30.  
  31.  
  32.  
I tried it on my machine and it worked fine :) I hope it worked for u as well
Apr 30 '14 #6

P: 4
thank you, it worked
Apr 30 '14 #7

Post your reply

Sign in to post your reply or Sign up for a free account.