|  |
03-29-2008, 04:08 AM
|
#1 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Perth, Australia Posts: 328
| IDLE help. Well, for school, we're still using Python and so I'm real new at it.
but; is there anyway I can put the bolded part of the code below, into a smaller piece of code?
I've heard of a loop or something?
Sorry I'm a noob. :|
but thanks xD
Oh, and the letters like, t and z etc will be changed, they're just there because I needed them quickly
def calc_check_digit(number):
number=str(number) a=number[-1]
b=number[-2]
c=number[-3]
d=number[-4]
e=number[-5]
f=number[-6]
g=number[-7]
h=number[-8]
i=number[-9]
j=number[-10]
k=number[-11]
l=number[-12]
m=number[-13]
n=number[-14]
o=number[-15]
p=number[-16]
a=a
b=b*2
c=c
d=d*2
e=e
f=f*2
g=g
h=h*2
i=i
j=j*2
k=k
l=l*2
m=m
n=n*2
o=o
p=p*2
if number > 9:
number=int(number)
number - 9
z=a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p
z=int(number)
t=z % 10
print t
__________________  InfernoDeals
92% of teens have moved on to rap. If you are part of the 8% who listen to real music, copy this. If you're someone who has faith that Mike has the strength to win this battle copy this to your sig. Some girl I know: im on a wireless network so i cant find my router Sylus: The thing with numbers on it is my binary blanket. |
| |
03-29-2008, 06:22 AM
|
#2 (permalink)
|
Join Date: Jun 2003 Location: Melbourne, Australia Posts: 13,739
| Re: IDLE help. I haven't learned python, but:
b = a-1
c = a-2
etc..
__________________ 1 + 1 = 3 if you define 3 as a result of 1 + 1 |
| |
03-29-2008, 08:15 AM
|
#3 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Perth, Australia Posts: 328
| Re: IDLE help. That wouldn't work
The [-1] [-2] [-3] etc, is the letter of the string, like..character value? or something.
So if I had a word that said "alphabet" and called it x, so x=alphabet.
z=x[0] would make z equal the a in alphabet.
z=x[1] would make z equal the p in alphabet.
z=[-1] would make z equal the t in alphabet.
That's a quick python lesson :| from a noob. I doubt it made sense.
Anyway,
Thanks for trying =D
__________________  InfernoDeals
92% of teens have moved on to rap. If you are part of the 8% who listen to real music, copy this. If you're someone who has faith that Mike has the strength to win this battle copy this to your sig. Some girl I know: im on a wireless network so i cant find my router Sylus: The thing with numbers on it is my binary blanket. |
| |
03-29-2008, 08:13 PM
|
#4 (permalink)
|
01001100011011110110110 Join Date: Mar 2007 Location: Perth, Australia Posts: 2,001
| Re: IDLE help. For a start, you don't need to include parts like e=e and a=a, that cuts you down 8 lines.
As for the first A-P definitions line, you'll have to keep it like that anyway, a loop wouldn't work because you have to define the variables.
There might be a way to cut down the *2 bits, I'll think about it and get back to you if I get anything
__________________ "As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a dour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams Click this if I helped you >>>> <<<< |
| |
03-29-2008, 10:01 PM
|
#5 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Perth, Australia Posts: 328
| Re: IDLE help. Good Point =D
Thanks,
Oh, and if you're curious...
It's part of a program to find out a IBM Check Digit
__________________  InfernoDeals
92% of teens have moved on to rap. If you are part of the 8% who listen to real music, copy this. If you're someone who has faith that Mike has the strength to win this battle copy this to your sig. Some girl I know: im on a wireless network so i cant find my router Sylus: The thing with numbers on it is my binary blanket. |
| |
03-30-2008, 12:02 AM
|
#6 (permalink)
|
01001100011011110110110 Join Date: Mar 2007 Location: Perth, Australia Posts: 2,001
| Re: IDLE help. I took a look at the IBM thing, why are you assigning each individual number a variable?
Try assigning the whole string of numbers (e.g. 6234 6243 2093 1029) one variable, say 'number'.
I'll just work on the code and post back if it works (i has got an idea!)
__________________ "As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a dour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams Click this if I helped you >>>> <<<< |
| |
03-30-2008, 12:49 AM
|
#7 (permalink)
|
Join Date: Jun 2003 Location: Melbourne, Australia Posts: 13,739
| Re: IDLE help. in Java, there are shortcuts to adding, dividing, multiplying or subtracting from a variable. I don't know if Python has something equivalent.
a = a * 2;
is the same as
a *= 2;
same with other operations
a += 2;
a -= 2;
a /= 2;
a %= 2;
or there's operations like
a++; increases a by 1
a--; decreases a by 1
__________________ 1 + 1 = 3 if you define 3 as a result of 1 + 1 |
| |
03-30-2008, 12:57 AM
|
#8 (permalink)
|
01001100011011110110110 Join Date: Mar 2007 Location: Perth, Australia Posts: 2,001
| Re: IDLE help. same with C, but I don't think python has those.
Ok, update. Basically what I tried was:
number=[5,1,3,2,4,5,6,7,2,3,4,5,6,4,5,6]
number1=number[1]*2,number[3]*2 etc.
the stuff-up came when I tried checking (and taking away) 9 from the values.
__________________ "As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a dour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams Click this if I helped you >>>> <<<< |
| |
03-30-2008, 02:47 AM
|
#9 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Perth, Australia Posts: 328
| Re: IDLE help. No, python doesn't.
and it uses += as not equal to, aswell as !=
__________________  InfernoDeals
92% of teens have moved on to rap. If you are part of the 8% who listen to real music, copy this. If you're someone who has faith that Mike has the strength to win this battle copy this to your sig. Some girl I know: im on a wireless network so i cant find my router Sylus: The thing with numbers on it is my binary blanket. |
| |
03-30-2008, 02:49 AM
|
#10 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Perth, Australia Posts: 328
| Re: IDLE help. Oh, I'm assigning each individual character because I need to multiply every second number by 2. not the whole string.
I've tried with this method as well, but I can't make the second number add the units together. MSI Barcode - Wikipedia, the free encyclopedia
__________________  InfernoDeals
92% of teens have moved on to rap. If you are part of the 8% who listen to real music, copy this. If you're someone who has faith that Mike has the strength to win this battle copy this to your sig. Some girl I know: im on a wireless network so i cant find my router Sylus: The thing with numbers on it is my binary blanket. |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |