[Tutor] Python
Vanshika Sadhwani
monvansha at gmail.com
Wed Oct 20 18:35:19 EDT 2021
Hello Dear Sir/Ma’am,
I had a doubt about one of my college assignments. I will provide a bit of background the on the assigned task -
Tasks: In this assignment, you will write a complete program in Python that will repeatedly ask the user for a code (integer) and determine whether it is a valid Basic Code, a Positional Code or a UPC Code. It is possible that some integers may be valid for more than one of these codes; even all three! Your program should consist of two Python files: Assign2.py which handles all the input and output and code_check.py. Assign2.py should repeatedly prompt the user for input, a string of digits, and then call functions in code_check.py to determine whether the digits in the string correspond to one of the above codes, Basic, Position, UPC.
The program should have a list for each type of code and when it finds that the string is a certain type of code, it should add the string to the list for that type of code. If a string is not one of the three types of codes, it should add it to a separate list. The program should repeatedly prompt the user for a string until the user enters a string that is zero, i.e., “0”. The program should then output each list, as described below, when the user has finished. The file code_check.py should consist of three functions: one function for each type of code. Each function should take a string as a parameter, such as “2452834” or “033367258”, and determine whether it is valid code; the function should return True or False. For example, the function that checks whether an identification number is a UPC code should take “2452834” as input and return True. Your program will make use of expressions, decisions, input/output, loops, lists and functions in Python. You should name your program Assign2.py.
So I did do most of the coding for the assignment, however whenever I execute it, it doesn’t give me the output I need. I will put pictures of my code for the code_check.py file and Assign2.py file, Please let me know about the errors I am making
This is for code_check:
def basic_code(a):
a = list(map(int, a.strip()))
total = sum(a)
valid_code = total % 10
check_digit = a[+1]
if valid_code == check_digit:
return True
else:
return False
def positional_code(a):
total = 0
a = list(map(int, a.strip()))
for i in range(0, len(a)):
total += a[i]
valid_code = total % 10
check_digit = a[+1]
if valid_code == check_digit:
return True
else:
return False
def UPC(a):
total = 0
a = list(map(int, a.strip()))
for i in range(0, len(a)):
if i % 2 == 1:
total += 3 * a[i]
else:
total += a[i]
valid_code = total % 10
if valid_code == 0:
valid_code = 10 - valid_code
check_digit = a[+1]
if valid_code == check_digit:
return True
else:
return False
This is for Assign2:
from code_check import*
codes = []
valid_basic = []
valid_positional = []
valid_UPC = []
none = []
while True:
code = input("Please enter code (digits only) (enter 0 to quit): ")
if code == '0':
break
codes.append(code)
for code in codes:
if basic_code(code) is True:
valid_basic.append(code)
elif positional_code(code) is True:
valid_positional.append(code)
elif UPC(code) is True:
valid_UPC.append(code)
else:
none.append(code)
print("Summary")
print("Basic :" + ''.join(valid_basic))
print("Position :" + ''.join(valid_positional))
print("UPC :" + ''.join(valid_UPC))
print("None :" + ''.join(none))
This is what I am getting when I run the code
Please help me, I would really really appreciate it,
Thank you
Sincerely
Vanshika Sadhwani
More information about the Tutor
mailing list