-1
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コードがあることがわかっていますが、コピーできません。
改善したい機能コードをコードレビューに提出することができます。stackexchange –
7つの数字を入力するための関数を作成し、両方の場合にその関数を再利用します。 8番目の入力が必要なものは、今持っている行を保持するか、関数が8を求める必要があるかどうかを示すブール値を取るようにしてください。 –
http://codereview.stackexchange.com/でお尋ねします。 –