import math
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
w=0
while w == 5:
print("Would you like to *work out* a missing letter in a GTIN-8 code, or *check* a code?")
response = input(":")
if response == 'work out':
print("Input a 7 digit GTIN-8 code and I'll work out the 8th")
c1 = int(input("Enter FIRST number: "))
c2 = int(input("Enter SECOND number: "))
c3 = int(input("Enter THIRD number: "))
c4 = int(input("Enter FOURTH number: "))
c5 = int(input("Enter FIFTH number: "))
c6 = int(input("Enter SIXTH number: "))
c7 = int(input("Enter SEVENTH number: "))
y = (c1*3+c2+c3*3+c4+c5*3+c6+c7*3)
ru2=roundup(y)
GTIN8 = ru2-y
print("Your GTIN8 Code would be: "+str(c1)+str(c2)+str(c3)+str(c4)+str(c5)+str(c6)+str(c7)+str(GTIN8))
print("Wanna work out another?")
if response == 'check':
print("Input a 8 digit GTIN-8 code and I'll check if it's correct")
c1 = int(input("Enter FIRST number: "))
c2 = int(input("Enter SECOND number: "))
c3 = int(input("Enter THIRD number: "))
c4 = int(input("Enter FOURTH number: "))
c5 = int(input("Enter FIFTH number: "))
c6 = int(input("Enter SIXTH number: "))
c7 = int(input("Enter SEVENTH number: "))
c8 = int(input("Enter EIGTH number: "))
y = (c1*3+c2+c3*3+c4+c5*3+c6+c7*3)
ru2=roundup(y)
GTIN8 = ru2-y
if GTIN8 != c8:
print("Nope that product code is incorrect!")
reply=input("Want to know the correct answer to your code? Type yes if so: ")
if reply == 'yes':
print("The correct answer would have been: "+str(GTIN8))
if GTIN8 == c8:
print("That code is correct!")
问题在于我一次又一次地尝试使这段代码更小。
即使输入'response'作为字符串,也允许用户一次输入代码。
如果您还不知道,这是GTIN-8产品代码的代码,我知道有其他各种GTIN-8代码,但我不能这样做,更不用说复制了。
答案 0 :(得分:-1)
为了帮助您入门,这是一种减少行数的简单方法
c = [int(x) for x in input("Input a 8 digit GTIN-8 code and I'll check if it's correct").split("")]
现在,您可以使用c[n]
访问每个角色。