python nameerror什么意思_我用Python编写的GTIN程序中的“NameError”是什么意思?

请注意我是初学者。

下面的程序是关于计算GTIN数的。基本上,不起作用的部分是验证部分。程序应该在向用户解释的同时验证代码;然而,计算机似乎无法识别我在某些情况下使用的变量原因。那个验证选项只有在你刚刚计算了一个GTIN编号并且菜单在询问你是否想重新运行程序。我希望你能理解我的问题!非常感谢!以下是我的代码:print("Welcome. Do you want to start? (Y/N)")

answer = input()

while(answer == "Y"):

print("1. Calculate a GTIN number")

print("2. Validate Previous Code (Please note that this option is only valid if you have just calculated a GTIN number)")

print("3. End Program")

option = int(input())

def GTIN():

#Step 1

print("Enter the first digit of your GTIN code")

digit_1 = int(input())

print("Enter the second digit")

digit_2 = int(input())

print("Enter the third digit")

digit_3 = int(input())

print("Enter the fourth digit")

digit_4 = int(input())

print("Enter the fifth digit")

digit_5 = int(input())

print("Enter the sixth digit")

digit_6 = int(input())

print("Enter the seventh digit")

digit_7 = int(input())

#Step 2

total_1 = digit_1 * 3

total_2 = digit_2 * 1

total_3 = digit_3 * 3

total_4 = digit_4 * 1

total_5 = digit_5 * 3

total_6 = digit_6 * 1

total_7 = digit_7 * 3

#Step 3

final_total = total_1 + total_2 + total_3 + total_4 + total_5 + total_6 + total_7

#Step 4

import math

def roundup(final_total):

return int(math.ceil(final_total / 10.0) * 10)

final_total_2 = roundup(final_total)

GTIN_number_8 = final_total_2 - final_total

print("Your complete GTIN number is:", digit_1, digit_2, digit_3, digit_4, digit_5, digit_6, digit_7, GTIN_number_8)

return digit_1, digit_2, digit_3, digit_4, digit_5, digit_6, digit_7, GTIN_number_8

def validation(digits, totals):

print("Your previous GTIN code:", digits[0], digits[1], digits[2])

print("Firstly, the computer will collect all of the digits, then it will multiply them by either 3 or 1:")

print("The 1st digit: ", digit_1, "will be multiplied by 3")

print("The 2nd digit: ", digit_2, "will be mutliplied by 1")

print("The 3rd digit: ", digit_3, "will be multiplied by 3")

print("The 4th digit: ", digit_4, "will be mutliplied by 1")

print("The 5th digit: ", digit_5, "will be multiplied by 3")

print("The 6th digit: ", digit_6, "will be mutliplied by 1")

print("The 7th digit: ", digit_7, "will be multiplied by 3")

print("Secondly, the computer will add up all of the digits:")

print("1st digit + 2nd digit + 3rd digit + 4th digit + 5th digit + 6th digit + 7th digit")

print(total_1, "+", total_2, "+", total_3, "+", total_4, "+", total_5, "+", total_6, "+", total_7)

print("Total =", final_total)

print("Thirdly, the total will be rounded to the highest multiple of 10")

print("The total rounded =", final_total_2)

print("Lastly, the total will then be subtracted from the rounded number to give the GTIN number 8")

print("Final GTIN number including the final digit:", digit_1, digit_2, digit_3, digit_4, digit_5, digit_6, digit_7, GTIN_number_8)

def end():

print("Ending Program")

if(option == 1):

GTIN(digits, totals)

elif(option == 2):

validation(digits)

elif(option == 3):

end()

else:

print("Ending Program")

print("Do you want to re-run the program? (Y/N)")

answer = input()

print("Ending Program")

“名称错误”消息是:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值