03-30-2008, 11:24 PM | #1 |
QuickShifter
145
Rep 1,499
Posts |
Python Programming
anyone here knows how to do python?
I'm trying to do this simple fuction input: num1 + num = results but i want the output be in pictures, so i decided to split num1 and num2 into digits by itself, so that i can apply the picture files to represent each number. now the problem is, after i split them into single digits, i can't get rid of the zero on the left side. ex. 01 + 01 = 2 ( i only split num1 and num2, so the results won't show 02) and, do i need to split results into single digits too? I believe doing that to the results would make the situation alot more messy.... help is appreciated thanks, |
03-31-2008, 12:42 AM | #2 |
Second Lieutenant
183
Rep 266
Posts
Drives: 2019 M550i xDrive
Join Date: Sep 2005
Location: Chicago
|
Can you post some of your code so I can see what you mean? Are you trying to split the number using string parsing? How do you want to represent the output in pictures?
__________________
M550i xDrive
Mediterranean Blue with Ivory White Nappa, Gray Poplar, Driving Assistance Plus, Premium, Exec, Parking Assistance, Heated Steering, Front+Rear Heated Seats, 20" Individual V-Spoke wheels 759i, 19" Style 786M winter wheels, Euro Amber Rear Turn Signals 335i Coupe - Retired |
Appreciate
0
|
03-31-2008, 12:46 AM | #3 | |
Member
4
Rep 47
Posts |
Quote:
$ python Python 2.4.3 (#1, Jul 4 2006, 23:58:18) [GCC 4.0.2 20050901 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> "02" + "02" '0202' >>> 2 + 2 4 >>> "02" + 2 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> So, you may need to convert things to and from strings and integers. To go from string -> int use the builtin int() function, and to go from int to string you can use something like "%s" % 2 (or just put the integer or var in backticks also works).
__________________
e90 335xi / 6MT / titanium silver / black leather / premium / cold weather / sport / iPod
|
|
Appreciate
0
|
03-31-2008, 01:01 AM | #4 |
QuickShifter
145
Rep 1,499
Posts |
i'm very noob with python, so my codes might look weird
import cgi form = cgi.FieldStorage() zero = open ( "0.jpg" ) one = open ( "1.jpg" ) two = open ( "2.jpg" ) three = open ( "3.jpg" ) four = open ( "4.jpg" ) five = open ( "5.jpg" ) six = open ( "6.jpg" ) seven = open ( "7.jpg" ) eight = open ( "8.jpg" ) nine = open ( "9.jpg" ) num1 = int( form["num1"].value ) num2 = int( form["num2"].value ) op = form["op"].value first_digit = num1 / 10 second_digit = num1 - (10*first_digit) secondfirst_digit = num2 / 10 secondsecond_digit = num2 - (10*secondfirst_digit) print """Content-type: text/html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head> <title>Assignment 2</title> </head><body> """ if num1 > 99 or num1 < 0 or num2 > 99 or num2 < 0 : print "Error: Wrong number." else: if op == "+" : print "%s%s + %s%s = %s" % (first_digit, second_digit, secondfirst_digit, secondsecond_digit, num1 + num2) if op == "-" : print "%s%s - %s%s = %s" % (first_digit, second_digit, secondfirst_digit, secondsecond_digit, num1 - num2) if op == "*" : print "%s%s * %s%s = %s" % (first_digit, second_digit, secondfirst_digit, secondsecond_digit, num1 * num2) if op == "/" and num2 != 0 : print "%s%s / %s%s = %s" % (first_digit, second_digit, secondfirst_digit, secondsecond_digit, num1 / num2) if op == "/" and num2 == 0: print "Error: Undefined." if op == "+" and first_digit == 0 : num1 = second_digit if op == "+" and secondsecond_digit == 0 : num2 = secondsecond_digit if op == "-" and first_digit == 0 : num1 = second_digit if op == "-" and secondsecond_digit == 0 : num2 = secondsecond_digit if op == "*" and first_digit == 0 : num1 = second_digit if op == "*" and secondsecond_digit == 0 : num2 = secondsecond_digit if op == "/" and first_digit == 0 : num1 = second_digit if op == "/" and secondsecond_digit == 0 : num2 = secondsecond_digit print "</body></html>" dont' mind the indentations, copy and paste doesn't copy indents too i guess |
Appreciate
0
|
03-31-2008, 01:08 AM | #5 |
QuickShifter
145
Rep 1,499
Posts |
|
Appreciate
0
|
03-31-2008, 04:27 PM | #6 |
Gangsta status
78
Rep 973
Posts
Drives: 2002 E39 M5, heavily modded
Join Date: May 2007
Location: Shibuya Tokyo, Rockland NY, Killeen TX
|
python man or man I haven't used that for awhillleee. Only used python for PERL
__________________
-TheLegacy
JDM-EDM-TT-V8-GANGSTA ALL THE WAY *Modded* "Ready for the Apocalypse" |
Appreciate
0
|
04-02-2008, 01:14 AM | #8 |
Member
4
Rep 47
Posts |
Ok, think I understand what you're trying to do. You probably don't want to open the actual files, but instead just put an html image tage, like
<img src="1.jpg"> ...in the output. Your logic with the division by 10 to get the first digit and then subtracting that off will work, but only if the data coming back from the cgi form (like form["num1"].value) is in numeric form...I'd expect it to actually be string data that comes back. If that's the case, then it's much easier to just do: first_digit = form["num1"].value[0] second_digit= form["num1"].value[1] ...since strings can be treated like lists. Then you could just do print '<img src="%s.jpg">' % first_digit print '<img src="%s.jpg">' % second_digit ...to output the first number's images (and do the second number similarly). Does this make any sense? : )
__________________
e90 335xi / 6MT / titanium silver / black leather / premium / cold weather / sport / iPod
|
Appreciate
0
|
04-02-2008, 01:31 AM | #9 | |
QuickShifter
145
Rep 1,499
Posts |
Quote:
btw, i have a HTM file linked to this python, so do i still need to do <> for hte img src? one more problem, i want to get rid of the zero on the first_digit ...'cause the equation comes out like 01+01 = 2, and i want to make it to 1 + 1 = 2 |
|
Appreciate
0
|
04-02-2008, 02:49 PM | #10 |
Member
4
Rep 47
Posts |
Assuming again that the form data comes across as a string (I still haven't checked this but think it's true) you can just
do a regex to strip off the leading 0, like: >>> import re >>> >>> teststr = '02' >>> teststr = re.sub(r'^0', '', teststr) >>> print teststr 2 I'm not sure offhand if you need the <>'s or not in your system. Try it both ways and see which works.
__________________
e90 335xi / 6MT / titanium silver / black leather / premium / cold weather / sport / iPod
|
Appreciate
0
|
Post Reply |
Bookmarks |
Thread Tools | Search this Thread |
|
|