Hi,

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

Two versions of the script needs to be made, one that adds and one that subtracts, but also from right to left and left to right... any how, if you cna let me know!

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

Recommended Answers

All 3 Replies

So can I see this VB script? Then I'll try and see what I can do about it :)

Hi.

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 stab 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()
f.close()
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.append(int(i))
# c should now be a LIST of simple integers [23, 2244, ...]
print "c is: ",c

c.reverse()
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()
f.close()

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')
f.write(result_str)
f.close()

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

I hope this helps.
Neal C.

f = open('numbers.txt','r') 
#read in the file, elminate all ", ', the leading and trailing spaces, split by whitespace to a list, and convert to int
numbs=map(int,f.read().replace('"','').replace('\'','').strip().split())
f.close()

f = open('modnumber.txt','r')
mod=int(f.read().replace('"','').replace('\'','')) # read in the file and convert into int
f.close()

total=sum(numbs) # sum numbs up

f=open('result.txt','w') 
f.write(str(total%mod)) # write out result
f.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.