-1

The question is:

Write a python function is_valid that checks if the input is a valid student id number. Valid student id’s are in the range 1000-6999 and the sum of their digits should be divisible by 7. If the id number is valid return True otherwise return False.

This code returns an undefined error. I try many ways to fix the issue however, nothing I think of works.

def is_valid():
    ID=input ("Enter ID number:")
if ID in range(1000,6999):
    ID= [[i for i in ID]for ID in input().split()]
    sum [ID]
if sum [ID]%2==0:
    print (True)
else:
    False
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • ``input`` provides a *string*. There is no way for a string to be in a ``range``, so the first ``if`` is skipped. Note that ``sum [ID]`` is bogus either way – ``sum(ID)`` would be correct. – MisterMiyagi Feb 17 '22 at 14:35
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – MisterMiyagi Feb 17 '22 at 14:38
  • Thank you for your quick response, I see there is a lot more wrong with this code than I thought. – The Outcast Feb 17 '22 at 14:56
  • Yeah, this was a lot of help. Thank you very much – The Outcast Feb 17 '22 at 15:09

3 Answers3

1

At the first look i can see the indentation is not correct, you are taking the number in input as a string then matching it against a range without converting it to int. Many more errors are there, so i just wrote a fresh code for you. Give it a go.

# is_valid function taking ID as an argument
def is_valid(ID):
    #checking the range after changing it to int()
    if int(ID) in range(1000, 7000):
        #adding all the digits in a string
        digits = list(ID)
        #adding the digits after mapping them to int()
        sum_of_digits = sum(list(map(int, digits)))
        if sum_of_digits%7==0:
            #returning true if the sum is divisible by 7
            return True
    return False
print(is_valid(input()))
anantdark
  • 31
  • 6
0

First, the error isn't undefined. You get NameError: name 'ID' is not defined. because the indentation is incorrect. Your program doesn't know the variable ID, since the if-blocks are outside of the function.

Then, the correct range 1000-6999 is range(1000,7000).

input returns a string whereas range(x) is a range generator containing integers.

So you have to parse ID to an int to check if it's in that range.

input().split() asks for a new input. Don't see why this is here.

If you want to enter each ID manually, this would be a correct answer:

def is_valid():
    ID = input("Enter ID number: ")
    if int(ID) in range(1000,7000):
        digits = [int(i) for i in ID]
        if sum(digits) % 7 == 0:
            return True
    return False

print(is_valid())
0

There are many issues wrong with your code. First the, code is not properly formatted, the indentation does not follow the level of code. All code should be indented from from function declaration.

Second, Input returns a string and not a integer so you can't compare it to a range of integers like you did here,Also range does not include second number so you have to change it to 7000:

if ID in range(1000,6999):

You can you the map function to return the individual digits.

DIGITS = []
# append each digit to list
for digit in map(int, ID):
    DIGITS.append(digit)

Finally, you can check the sum of the digits and see if they are divisible by seven

# Check if sum of digits is divisible by seven
if sum(DIGITS) % 7 == 0:
    print(True)
# Not divisible by 7
else:
    print(False)

Here is the complete code:

def is_valid():
    # Obtain ID Number
    ID = input("Enter ID number:")
    if int(ID) in range(1000, 7000):
        # List for individual digit in input
        DIGITS = []
        # append each digit to list
        for digit in map(int, ID):
            DIGITS.append(digit)
        # Check if sum of digits is divisible by seven
        if sum(DIGITS) % 7 == 0:
            print(True)
        # Not divisible by 7
        else:
            print(False)
    # Not between 1000 inclusive and 7000 exclusive
    else:
        print(False)

is_valid()
Siesmic_
  • 71
  • 1
  • 5