Jump to content
Sign in to follow this  
Rallbot

GCSE code - feedback pls :)

Recommended Posts

Any feedback would be greatly appreciated, coding I'm working on python to take the seven digits on a barcode and create an eighth one, basically. Any ideas that would help improve/tidy up a bit would be great, thanks :)

(also, different sectioned off parts are updates on the code, bottom is the latest :D

 

 


 


GTIN = (input(" "))
length = len(GTIN)
if(length==7):
  GTIN_1 = int(GTIN[0])
GTIN_2 = int(GTIN[1])
GTIN_3 = int(GTIN[2])
GTIN_4 = int(GTIN[3])
GTIN_5 = int(GTIN[4])
GTIN_6 = int(GTIN[5])
GTIN_7 = int(GTIN[6])
GTIN_ = int((GTIN_*3)+GTIN_1+(GTIN_2*3)+GTIN_3+(GTIN_4*3)+GTIN_5+(GTIN_6*3))
roundup = round(GTIN_, -1)
GTIN8 = int(roundup-GTIN_)
print (" " + str(GTIN) + str(GTIN8))

#=====================================================================#

GTIN = (input(" "))
length = len(GTIN)
if(length==7):
  a = int(GTIN[0])
b = int(GTIN[1])
c = int(GTIN[2])
d = int(GTIN[3])
e = int(GTIN[4])
f = int(GTIN[5])
g = int(GTIN[6])
GTIN_ = int((a*3)+b+(c*3)+d+(e*3)+f+(g*3))
roundup = round(GTIN_, -1)
GTIN8 = int(roundup-GTIN_)
print (" " + str(GTIN) + str(GTIN8))

#=====================================================================#

GTIN = (input("Enter the 7 digit number... ")) #Added text to input + print function.
length = len(GTIN)
if(length==7):
  a = int(GTIN[0])
b = int(GTIN[1])
c = int(GTIN[2])
d = int(GTIN[3])
e = int(GTIN[4])
f = int(GTIN[5])
g = int(GTIN[6])
GTIN_ = int(a*3+b+c*3+d+e*3+f+g*3) #Removed unecessary brackets.
roundup = round(GTIN_, -1)
GTIN8 = int(roundup-GTIN_)
print ("The full GTIN-8 code is... " + str(GTIN) + str(GTIN8))

#=====================================================================#

GTIN = (input("Enter the 7 digit number... "))
length = len(GTIN)
if(length==7):
  a = int(GTIN[0])
b =int(GTIN[1])
c = int(GTIN[2])
d = int(GTIN[3])
e = int(GTIN[4])
f = int(GTIN[5])
g = int(GTIN[6])
GTIN_ = int((a*3)+b+(c*3)+d+(e*3)+f+(g*3))
#roundup=round(GTIN_, -1)   
#GTIN8=int(roundup-GTIN_)   # not all numbers worked - errors (negative int issue)   
GTIN8 = (GTIN_ + 9) // 10 * 10 - GTIN_
print("The full GTIN-8 code is... "+str(GTIN)+str(GTIN8))

#=====================================================================#

GTIN = (input("Enter the 7 digit number... "))
length = len(GTIN)
if(length==7):
  a = int(GTIN[0])
b = int(GTIN[1])
c = int(GTIN[2])
d = int(GTIN[3])
e = int(GTIN[4])
f = int(GTIN[5])
g = int(GTIN[6])
GTIN_ = int((a*3)+b+(c*3)+d+(e*3)+f+(g*3))
GTIN8 = (GTIN_ + 9) // 10 * 10 - GTIN_
GTIN_F = str(GTIN) + str(GTIN8) 
print("The full GTIN-8 code is... "+str(GTIN_F))

#=====================================================================#

GTIN = (input("Enter the 7 digit number... "))
length = len(GTIN)
if(length==7):
  a=int(GTIN[0])
  b=int(GTIN[1])
  c=int(GTIN[2])
  d=int(GTIN[3])
  e=int(GTIN[4])
  f=int(GTIN[5])
  g=int(GTIN[6])
  GTIN_ = int((a*3)+b+(c*3)+d+(e*3)+f+(g*3))
  GTIN8 = (GTIN_ + 9) // 10 * 10 - GTIN_
  GTIN_F = str(GTIN) + str(GTIN8) 
  print("The full GTIN-8 code is... " + str(GTIN_F))
else:
  print ("Invalid") #added alt. for any errors.

#=====================================================================#
  
  GTIN = (input("Enter the 7 digit number... "))
length = len(GTIN)
while length==7: #infinite number expansion (does not work!)
  print ("valid")
  if(length==7):
    a = int(GTIN[0])
    b = int(GTIN[1])
    c = int(GTIN[2])
    d = int(GTIN[3])
    e = int(GTIN[4])
    f = int(GTIN[5])
    g = int(GTIN[6])
    GTIN_ = int((a*3)+b+(c*3)+d+(e*3)+f+(g*3))
    GTIN8 = (GTIN_ + 9) // 10 * 10 - GTIN_
    GTIN_F = str(GTIN) + str(GTIN8) 
    print("The full GTIN-8 code is... "+str(GTIN_F))
    GTIN = str(GTIN_F) #made to prevent forementioned. (does not work)
  else:
    print ("Invalid")

#=====================================================================#

    GTIN = (input("Enter the 7 digit number... "))
length = len(GTIN)
if(length==7):
  a,b,c,d,e,f,g = int(GTIN[0,1,2,3,4,5,6]) #made to shorten code (does not work)(WIP)
  GTIN_ = int((a*3)+b+(c*3)+d+(e*3)+f+(g*3))
  GTIN8 = (GTIN_ + 9) // 10 * 10 - GTIN_
  GTIN_F = str(GTIN) + str(GTIN8) 
  print("The full GTIN-8 code is... "+str(GTIN_F))
else:
  print ("Invalid")

 

Edited by Rallbot
CodeBox Added

Share this post


Link to post
Share on other sites

hmm, wow, lemme take a look at this lol. What exam board are you with?? I'm doing edexcel, got a totally diffrent task lol

 

 

Hmm, maybe putting all of the diffrent variables into a list since the "remade to shorten code" bit doesnt work, for example, when using the variables you can be like (list[0])*(list[1]) or what ever it is that you need to do for the task

  • Like 1

Share this post


Link to post
Share on other sites
GTIN = input("Enter the 7 digit number... ")
length = len(GTIN)
if length == 7:
    GTIN = list(GTIN)
    GTIN_ = ((int(GTIN[0])*3)+int(GTIN[1])+(int(GTIN[2])*3)+int(GTIN[3])+(int(GTIN[4])*3)+int(GTIN[5])+(int(GTIN[6])*3))
    GTIN8 = (GTIN_ + 9) // 10 * 10 - GTIN_
    strGTIN = ''.join(GTIN)
    GTIN_F = str(strGTIN) + str(GTIN8)
    print("The full GTIN-8 code is... "+str(GTIN_F))
else:
    print ("Invalid")

I don't know if this is what you wanted but it works and seems more condensed in a way.

  • Like 1

Share this post


Link to post
Share on other sites

Oh, thanks for the idea there. I'm not so great with python as it's not exactly ideal, that helps man :D

@Coach

 

On 2/28/2017 at 17:49, lukshan13 said:

hmm, wow, lemme take a look at this lol. What exam board are you with?? I'm doing edexcel, got a totally diffrent task lol

 

 

Hmm, maybe putting all of the diffrent variables into a list since the "remade to shorten code" bit doesnt work, for example, when using the variables you can be like (list[0])*(list[1]) or what ever it is that you need to do for the task

Right, here's the work sheet, all info we were given is on that only. If it helps you get an idea of what the task is :)

Edited by Rallbot

Share this post


Link to post
Share on other sites
On 04/03/2017 at 13:47, RAZZ said:

I have no idea what this means.

Well, i'm verging on the fact that i dont either - my brain only works when it needs to, then just stops so looking back I just think "what the hell is all this?!" :D x3

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...