464,516 Members | 943 Online
Need help? Post your question and get tips & solutions from a community of 464,516 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

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

6 Replies

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

 P: 4 no it bit like this Expand|Select|Wrap|Line Numbers {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)} Apr 30 '14 #3

 P: 7 Yeah, surely there is a way out .... so the idea is same .. we have to choose Expand|Select|Wrap|Line Numbers random.choice() 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 random.choice() function .... in ur case we can do it as : Expand|Select|Wrap|Line Numbers import random random_choice=random.choice(str(x)) Apr 30 '14 #4

 P: 4 < 2> Traceback (most recent call last): File "C:\Users\-\main.py", line 18, in 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 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 )     Apr 30 '14 #5

 P: 7 this is probably because Expand|Select|Wrap|Line Numbers 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 random_choice = int(random.choice(str(x))) and your overall code should go like this : Expand|Select|Wrap|Line Numbers from random import randint import sys import random     x = int(raw_input('Enter a four digit number:')) random_choice = int(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 )       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