Nav
Nav

Reputation: 1

For some reason after I choose an option it repeats and keeps asking for what option I want to choose again

Part 1. A checkdigits module that accepts an integer representing a 12-digit UPC code or a 10-digit ISBN code, and calculates and compares the check digit in a UPC or ISBN code to determine whether it is a valid code or not.

Part 2. A Python program that uses the functions defined by the checkdigits module to allow user to validate ISBN and UPC codes.

File 1- the Module

def get_digit(number, position):
  return number / 10 ** position % 10
  
def is_UPC12(number):
  odd_sum = 0
  even_sum = 0
  for digit in range (1, 12, 2):
    odd_sum += get_digit (number, digit)
  for digit in range (2, 11, 2):
    even_sum += get_digit (number, digit)
  if 10 - (odd_sum * 3 + even_sum) % 10 == get_digit(number, 0):
    return True
  else:
    return False
    
def is_ISBN10(number):
  total = 0
  multiple = 9
  for digit in range (1, 10):
    total += get_digit (number, digit) * multiple
    multiple -= 1
  if total % 11 == get_digit (number, 0):
    return True
  else:
    return False

File 2

import checkdigits
print ("CHECK DIGIT VALIDATOR")
def main():
  option = "1"
  while option == "1" or option == "2":
    print ("What would you like to validate: ")
    print ("1. ISBN-10\n2. UPC-12\nQ. Quit Program")
    option = input()
    
  while option != "1" and option != "2" and option != "Q" and option != "q":
    print ("Invalid option. Please choose again.")
    print ("What would you like to validate: ")
    print ("1. ISBN-10\n2. UPC-12\nQ. Quit Program")
    option = input()
    
    if option == "Q" or option == "q":
      break
  validation_process(option)
  
def validation_process(option):
  if option == "1":
    code = input ("Enter your ISBN code to be validated: ")
    if checkdigits.is_ISBN10(code) == True:
      print ("Your code is valid.")
    else:
      print ("Your code is invalid.")
  elif option == "2":
      code = input ("Enter your UPC code to be validated: ")
      if checkdigits.is_UPC12(code) == True:
        print ("Your code is valid.")
      else:
        print ("Your code is invalid.")
  else:
    print ("Please enter a valid input.")
main()

Well I didn't try anything to fix this problem because I'm new to coding and this is my second assignment. I had some help from my classmates creating this code but even they don't know what is wrong with it.

Upvotes: 0

Views: 52

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

The PROBLEM here is the way you wrote your first loop:

  option = "1"
  while option == "1" or option == "2":
    print ("What would you like to validate: ")
    print ("1. ISBN-10\n2. UPC-12\nQ. Quit Program")
    option = input()

Look at while options. As long as you keep making a valid input, it will keep looping. It will not exit until you make an invalid input.

Here is a better way to write that:

import checkdigits
print ("CHECK DIGIT VALIDATOR")
def main():
  while True:
    print ("What would you like to validate: ")
    print ("1. ISBN-10\n2. UPC-12\nQ. Quit Program")
    option = input()
    if option in 'Qq':
      return
    if option in '12':
      validation_process(option)
    else:
      print ("Invalid option. Please choose again.")
  
def validation_process(option):
  if option == "1":
    code = input ("Enter your ISBN code to be validated: ")
    valid = checkdigits.is_ISBN10(int(code))
  elif option == "2":
    code = input ("Enter your UPC code to be validated: ")
    valid = checkdigits.is_UPC12(int(code))
  else:
    print ("Please enter a valid input.")
    return

  if valid:
    print ("Your code is valid.")
  else:
    print ("Your code is invalid.")

main()

Note that I have removed all the repeated sections. Questions are asked once, instead of multiple times. Also note that your "checkdigits" code requires an integer, not a string.

Upvotes: 2

Related Questions