469,946 Members | 1,784 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

Help with simple math problem


I'm new to Python and am hoping to find help with coding a Python script, applet.

I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long
it does some relatively simple math additions and subtractions

The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is to have all numbers added and subtracted with exact precision even when the numbers become very big, the code sums, subtracts these numbers so that in the end there is a resultant, often again a very large number

I need to be able to perform a mod function on this end resultant

here is how it will work ;

1.) Python script opens a text file, the text file contains a variable number of numbers in sequence with spaces in between them, such as

" 23 2244 87 62 71 99 8 4 9 344 21 77 87 63 98 56 64 33 22"

The number of numbers in this text file can range from only a few to hundreds

Python scoops these out and stores and processes these as per my code in VB, but it retains the precision of the very large numbers, NOT converting these to scientific notation

2.) The end result is obtained and a mod function is used on it, the mod value can vary as well to be any number, this mod value is stored in a text file, the python script opens this text file and loads the value as a variable when ever it runs and uses that variable as the value of the mod divisor

3.) the mod value of the resultant is then printed out to a text file

Thats it!

I have the VB code and can provide it, what Im hoping for is to ask someone to convert its exact functionality, one to one to Python script

Four versions of the code needs to be made, one that adds and one that subtracts, but also from right to left and left to right... to explain better, four versions are needed;

1.) Add number sequence from left to right
2.) Add number sequence from right to left
3.) Subtract number sequence from left to right
4.) Subtract number sequence from right to left

I want to be able to call the python script from VB, so the Python dos command screen activates or at least runs and does its thing

I am willing to offer some cash for this help, if it works properly but I dont have a lot of money as I am unemployed
I can offer $50.00 and can pay you via paypal

Thanks for any help you can offer
Aug 14 '08 #1
2 1877
849 Expert 512MB
You posted the same thing over in the C/C++ forum, which language do you really want this in?
Aug 14 '08 #2

I'm also trying to figure out how to use Python - there are some nice books out there, but finding a Class is harder. And asking a book can be pretty frustrating. (Even asking a person can be tough...)

If writing the Python code was the easy part, then this reply will probably be pretty useless. I don't know VB, nor even how to make a python module into an applet or standalone app.

Anyway, I took a hack at part of your problem (using IDLE with MacPython 2.5.1 on Mac OS 10.4.11).

I started with 3 files on my desktop:
addemup.py (the python module, below)
numbers.txt (the numbers in a text file - I used yours and 1000000000000000000)
modnumber.txt (the modulus in a text file)

I am not sure if your text file contain numbers separated by spaces:
23 2244 87 . . . or if it contained a single quoted string:
"23 2244 87 ... "
So I wrote it to handle either way.

Here 'tis:

# addemup.py (located on my desktop, along with
# numbers.txt - a file listing your numbers separated by spaces, and
# modnumber.txt - a file listing the modulus: I picked 100)

f = open('numbers.txt','r')
a_str = f.readline()
print "a_str contains: ",a_str

if a_str[0] == "'": # if the numbers in numbers.txt are surrounded by single quotes, remove the quotes
a_str = a_str.strip("'")
if a_str[0] == '"': # if the numbers in numbers.txt are surrounded by double quotes, remove the quotes
a_str = a_str.strip('"')
# a_str should now be a string of numbers separated by spaces
print "a_str now contains: ",a_str

b = a_str.split()
# b should now be a LIST of quoted integers ['23', '2244', ... ]
print "b is: ",b

c = [ ]
for i in b:
# c should now be a LIST of simple integers [23, 2244, ...]
print "c is: ",c

print "c is now: ",c # in case you want to

total = 0
for i in c:
total = total + i
print "total is ",total

f = open('modnumber.txt','r')
m_str = f.readline()

if m_str[0] == "'": # if the number in mod.txt are surrounded by single quotes, remove the quotes
m_str = m_str.strip("'")
if m_str[0] == '"': # if the number in mod.txt are surrounded by double quotes, remove the quotes
m_str = m_str.strip('"')
# m_str should now be a string containing the modulus
print "m_str contains: ", m_str

m = int(m_str)
# m is now the actual integer modulus
print "m is: ",m

result_str = str(total % m)
print "result is",result_str

f = open('result.txt','w')

# The file result.txt should appear on the desktop and contain a 72 (no quotes)

I hope this helps.
Neal C.
Aug 18 '08 #3

Post your reply

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

Similar topics

38 posts views Thread by Jordi | last post: by
11 posts views Thread by frankie_85 | last post: by
9 posts views Thread by Verizon News Server | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.