Author |
Message |
silvermatt23
|
Posted: Wed Feb 06, 2013 8:27 pm Post subject: help with making random operators in python |
|
|
im working on a program in pythonthat will generate random numbers and a random math opperator. and then it will solve the the problem. i dont know to generate the opperator though. can some one help? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Sur_real
|
Posted: Wed Feb 06, 2013 8:34 pm Post subject: RE:help with making random operators in python |
|
|
You can just map an unique integer to each operator and generate a random integer which will correspond to that operator. |
|
|
|
|
![](images/spacer.gif) |
silvermatt23
|
Posted: Wed Feb 06, 2013 10:43 pm Post subject: RE:help with making random operators in python |
|
|
Thanks, but I don't know how to do that. Can u give me like an example. It will be very helpful thanks |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Wed Feb 06, 2013 11:36 pm Post subject: RE:help with making random operators in python |
|
|
If 0 means "add" and 1 means "subtract", then 2 means... |
|
|
|
|
![](images/spacer.gif) |
silvermatt23
|
Posted: Thu Feb 07, 2013 1:50 am Post subject: RE:help with making random operators in python |
|
|
Ok but do they have to be strings or can I just type in 0=+ etc? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Thu Feb 07, 2013 1:30 pm Post subject: RE:help with making random operators in python |
|
|
What do you think they should be? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Sat Feb 09, 2013 2:18 am Post subject: Re: help with making random operators in python |
|
|
This should get you started...
Python: | >>> ops = [lambda x, y: x + y, lambda x, y: x - y]
>>> ops[0](5, 5)
10
>>> ops[1](5, 5)
0 |
|
|
|
|
|
![](images/spacer.gif) |
|