-1

I type in seven digits to be able to work out a gtin-8 product code. But if I type in more than seven digits, the if len statement is meant to recognise that I have typed in more than seven digits, but it doesn't. I tried putting this into an variable but that did not work either... Any help would be appreciated!!! This is my code........

gtin1 = int(input("Enter your first digit... "))
gtin2 = int(input("Enter your second digit... "))
gtin3 = int(input("Enter your third digit... "))
gtin4 = int(input("Enter your fourth digit... "))
gtin5 = int(input("Enter your fifth digit... "))
gtin6 = int(input("Enter your sixth digit... "))
gtin7 = int(input("Enter your seventh digit... "))

gtin_join = (gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7) 

if len(gtin_join) == 7:
Lewis Graham
  • 13
  • 1
  • 3
  • 2
    Why do you have your program setup to enter one digit at a time? Why don't you just type in the entire code – Keatinge Jun 23 '16 at 08:21
  • of course it does not since each input() call, no matter what you type in (even 8,3,2,1) for example is interpreted as 1 thing. This this is of type string. – Ma0 Jun 23 '16 at 08:21
  • If you're going to enter one number at a time, you should also check each is a single digit. Alternatively, get the user to enter a single number and just check it's `< 10000000`. – jonrsharpe Jun 23 '16 at 08:33

3 Answers3

1

What you probably want to do is something like that (notice that I'm using a list here):

ls = []
while len(ls) < 7:
    try: #always check the input
        num = int(input("Enter your {0} digit:".format(len(ls)+1) ))
        ls.append(num)
    except: 
        print("Input couldn't be converted!")

print(ls) #ls now has 7 elements

Your created tuple always has a length of 7 so your if-statement always turns out to be True.

For the difference between list and tuple please see this question here.

Community
  • 1
  • 1
-1

Your gtin_join is tuple, and if you want list you should use square braces. You can test type of variables with this example:

gtin1 = int(input("Enter your first digit... "))
gtin2 = int(input("Enter your second digit... "))
gtin3 = int(input("Enter your third digit... "))
gtin4 = int(input("Enter your fourth digit... "))
gtin5 = int(input("Enter your fifth digit... "))
gtin6 = int(input("Enter your sixth digit... "))
gtin7 = int(input("Enter your seventh digit... "))

gtin_join = (gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7)

print(type(gtin_join))

gtin_join = [gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7]

print(type(gtin_join))

if len(gtin_join) == 7:
    print 7
Teemo
  • 449
  • 6
  • 23
-1

I would do the following:

gtin_list = []
while len(gtin_list) != 7:
    gtin = input("Please enter all 7 digits separated by commas...")
    gtin_list = [int(x) for x in gtin.split(",")]
Ma0
  • 15,057
  • 4
  • 35
  • 65