IDLE help.

Status
Not open for further replies.

sinci

In Runtime
Messages
327
Location
Perth, Australia
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 :p


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
 
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
 
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
 
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!) :)
 
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
 
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.
 
Status
Not open for further replies.
Back
Top Bottom