BMW X3 Forum
BMW X3 Forum
Welcome to the ultimate G45 BMW X3 community.
BMW Garage BMW Meets Register Today's Posts
Post Reply
 
Thread Tools Search this Thread
      03-30-2008, 11:24 PM   #1
ChineseGuy
QuickShifter
ChineseGuy's Avatar
145
Rep
1,499
Posts

Drives: Black E90
Join Date: Nov 2005
Location: North Pole

iTrader: (2)

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,
Appreciate 0
      03-31-2008, 12:42 AM   #2
bastula
Second Lieutenant
bastula's Avatar
United_States
183
Rep
266
Posts

Drives: 2019 M550i xDrive
Join Date: Sep 2005
Location: Chicago

iTrader: (1)

Garage List
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
twinturbodude
Member
twinturbodude's Avatar
United_States
4
Rep
47
Posts

Drives: 335xi
Join Date: Jun 2007
Location: MA

iTrader: (0)

Quote:
Originally Posted by ChineseGuy View Post
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,
Sounds like you may be having an issue with strings vs. integers. Python differentiates between "02" which is a string consisting of two characters, "0" and "2", and 2, which is an integer. The + operator does string concatenation if you are trying to add strings, numeric addition if you are trying to add numbers, and gives a runtime error if you attempt to add a string and a number. Like:

$ 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
ChineseGuy
QuickShifter
ChineseGuy's Avatar
145
Rep
1,499
Posts

Drives: Black E90
Join Date: Nov 2005
Location: North Pole

iTrader: (2)

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
ChineseGuy
QuickShifter
ChineseGuy's Avatar
145
Rep
1,499
Posts

Drives: Black E90
Join Date: Nov 2005
Location: North Pole

iTrader: (2)

Quote:
Originally Posted by twinturbodude View Post
Sounds like you may be having an issue with strings vs. integers.
i'm not sure if it's strings vs integers problem, looks correct to me

but please check out my codes and see if that's the problem

i've been stuck on this part for hours already
Appreciate 0
      03-31-2008, 04:27 PM   #6
TheLegacy
Gangsta status
TheLegacy's Avatar
United_States
78
Rep
973
Posts

Drives: 2002 E39 M5, heavily modded
Join Date: May 2007
Location: Shibuya Tokyo, Rockland NY, Killeen TX

iTrader: (0)

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
      03-31-2008, 08:22 PM   #7
ChineseGuy
QuickShifter
ChineseGuy's Avatar
145
Rep
1,499
Posts

Drives: Black E90
Join Date: Nov 2005
Location: North Pole

iTrader: (2)

anyone?
Appreciate 0
      04-02-2008, 01:14 AM   #8
twinturbodude
Member
twinturbodude's Avatar
United_States
4
Rep
47
Posts

Drives: 335xi
Join Date: Jun 2007
Location: MA

iTrader: (0)

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
ChineseGuy
QuickShifter
ChineseGuy's Avatar
145
Rep
1,499
Posts

Drives: Black E90
Join Date: Nov 2005
Location: North Pole

iTrader: (2)

Quote:
Originally Posted by twinturbodude View Post
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? : )
i think i get it, but i'll need to try it out first

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
twinturbodude
Member
twinturbodude's Avatar
United_States
4
Rep
47
Posts

Drives: 335xi
Join Date: Jun 2007
Location: MA

iTrader: (0)

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


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -5. The time now is 11:14 AM.




x3:
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
1Addicts.com, BIMMERPOST.com, E90Post.com, F30Post.com, M3Post.com, ZPost.com, 5Post.com, 6Post.com, 7Post.com, XBimmers.com logo and trademark are properties of BIMMERPOST