【发布时间】:2017-03-05 17:43:17
【问题描述】:
我在计算课上设置了一项任务,我必须开发一个 python 程序来生成和验证 GTIN-8 条形码。然而,我的老师很穷,他一点头绪都没有,所以在课堂上寻求帮助并不是一个真正有效的选择。此外,在设置这个作业之前,我的班级几乎没有 Python 经验,而我目前正是 Python 初学者的定义。无论如何,这是我到目前为止的所有代码:
def mainmenu():
print("1. Generate a barcode")
print("2. Validate a barcode")
print("3. Quit") #prints out 3 options for the user to select
while True: #loops over and over again
try: #handles exceptions
selection=int(input("Enter choice: ")) #tells user to enter a choice
if selection==1:
generate() #calls the function
break #terminates the loop
elif selection==2:
validate()
break
elif selection==3:
break
else:
print("Invalid choice. Enter 1-3")
mainmenu()
except ValueError: #if user enters a string, it will loop back
print("Invalid choice. Enter 1-3")
exit
def generate():
print("You have chosen to generate a barcode")
D1 = int(input("Please enter the first digit"))
D2 = int(input("Please enter the second digit"))
D3 = int(input("Please enter the third digit"))
D4 = int(input("Please enter the fourth digit"))
D5 = int(input("Please enter the fifth digit"))
D6 = int(input("Please enter the sixth digit"))
D7 = int(input("Please enter the seventh digit"))
timestogether = int('D1') * 3
print (timestogether)
anykey=input("Enter anything to return to main menu")
mainmenu()
def validate():
print("You have chosen to validate a barcode")
anykey=input("Enter anything to return to main menu")
mainmenu()
# recalls the main menu
mainmenu()
到目前为止,我已经开发了一个菜单,询问用户是否要生成或验证条形码。但是,我不确定下一步该做什么。如果用户想要生成条形码,程序必须要求用户输入一个七位数字,然后它必须将这七位数字交替乘以 3 而不是 1,然后必须将结果相加得到一个和,然后它必须从最接近的等于或大于 10 的倍数中减去总和。最后,结果将是第八位(校验位),因此程序应该输出最终的条形码。
如果用户想要验证条形码,程序应该要求用户输入一个八位数的代码。然后它应该用三个和一个重复上述乘法过程。然后它应该将所有计算出来的数字相加,如果结果是 10 的倍数,则条形码是有效的,它应该向用户输出一条消息,说明 GTIN-8 是有效的。但是,如果不是 10 的倍数,则条码无效,应该打印错误。
无论如何,感谢您抽出宝贵时间阅读本文,我们将不胜感激。
【问题讨论】:
标签: python