473,513 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random integers to have speficic value

4 New Member
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
6 1518
bvdet
2,851 Recognized Expert Moderator Specialist
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
adent
4 New Member
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
7 New Member
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
adent
4 New Member
<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
7 New Member
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
adent
4 New Member
thank you, it worked
Apr 30 '14 #7

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

Similar topics

1
3633
by: praba kar | last post by:
Dear All In Php we can find in_array() function which function is mainly useful to check whether a specific value is exist in the array or not. But In python In cannot find any function like...
0
3561
by: DraguVaso | last post by:
Hi, I'm using the DataGridView in VB.NET 2.0. The DataSource is a Generic List of a custom class0: lstMyPersonnes = New List(Of clsPersonne). When I add a new clsPersonne to lstMyPersonnes,...
36
5937
by: Michael B Allen | last post by:
Someone once posted the following macro on clc: #define randint(a,b) (a)+(((b)-(a)+1)*(float)rand()/RAND_MAX) Unfortunately it's flawed. If rand() returns RAND_MAX the result can be one larger...
6
5511
by: Julius Fenata | last post by:
Dear all, I have created client-side scripting to trigger event onChange from code-behind, like this: DropDownList1.Attributes = "GenerateArticleID()"; At the script on Windows Form, I...
4
1674
by: ina | last post by:
Hello all, I am newbie in xml and have a problem with this parse. I have this xml.file <Style> <Strategy>
5
2939
by: =?Utf-8?B?emlubw==?= | last post by:
in .net 2.0, the class (myClass) contains a property defined as: private _creationDate as As Nullable(Of DateTime) Public Property CreationDate() As Nullable(Of DateTime) Get If...
0
3463
by: Software Engineer | last post by:
Random Date and Time Value GeneratorIf http://sqa.fyicenter.com/Online_Test_Tools/Random_Date_Time_Value_Generator.php If you are software tester, you may need some random date and time values...
2
2620
by: Steve | last post by:
How can I fill an empty field (FieldA) in a table with random integers between 1 and 11? Thanks!
2
5262
by: shapper | last post by:
Hello, I am trying to define a value for a statistic as follows: FilesPerMonth = db.Files.Count() / (float)((DTH.Span(DI.Month, db.Files.Min(f => f.CreatedAt), DateTime.Now) ?? 1) == 0 ? 1 :...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7114
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5693
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5098
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.