需要python帮助ISBN计算器

时间:2014-02-16 17:12:33

标签: python

此处是我的代码:

decision = str(input(""" What would you like to do?; 
1) Convert a 10 digit number to an ISBN number 
2) Quit and end the programme""")) 

if decision == "2": 
    quit()

elif decision == "1": 
    ISBN=input("Enter a 10 digit number:") 

while len(ISBN)!= 10: 

    print('YOU DID NOT ENTER A 10 DIGIT NUMBER !!!') 
    ISBN=int(input('Please enter a 10 digit number: ')) 
    continue

else: 

    Di1=int(ISBN[0])*11
    Di2=int(ISBN[1])*10
    Di3=int(ISBN[2])*9
    Di4=int(ISBN[3])*8
    Di5=int(ISBN[4])*7
    Di6=int(ISBN[5])*6
    Di7=int(ISBN[6])*5
    Di8=int(ISBN[7])*4
    Di9=int(ISBN[8])*3
    Di10=int(ISBN[9])*2

sum=(Di1+Di2+Di3+Di4+Di5+Di6+Di7+Di8+Di9+Di10) 

num=sum%11
Di11=11-num 
if Di11==10: 
    Di11='X'
ISBNNumber=str(ISBN)+str(Di11) 
print('The ISBN number is -->    ' + ISBNNumber) 

我希望它循环,所以当我选择选择1时它给我11位数字我希望它循环回菜单询问我是否要输入10位数字或退出。 不应该太难,但我已经花了太长时间,只是找不到修复。

谢谢

2 个答案:

答案 0 :(得分:0)

替换此代码:

if decision == "2": 
    quit

有了这个:

if decision == "2": 
    quit() 

你忘了quit之后的括号。

答案 1 :(得分:0)

尝试将整个事物包装在while循环中,如下所示:

while True: # will loop until user enters "2" to break the loop

    decision = str(input(""" What would you like to do?; 
    1) Convert a 10 digit number to an ISBN number 
    2) Quit and end the programme""")) 

    if decision == "2": 
        break # escape the loop, effectively jumping down to the quit()

    elif decision == "1": 
        ISBN=input("Enter a 10 digit number:") 

        while len(ISBN)!= 10: # fixed indentation here...

            print('YOU DID NOT ENTER A 10 DIGIT NUMBER !!!') 
            ISBN=int(input('Please enter a 10 digit number: ')) 
            continue 

        # I don't believe the else clause should have been here
        Di1=int(ISBN[0])*11
        Di2=int(ISBN[1])*10
        Di3=int(ISBN[2])*9
        Di4=int(ISBN[3])*8
        Di5=int(ISBN[4])*7
        Di6=int(ISBN[5])*6
        Di7=int(ISBN[6])*5
        Di8=int(ISBN[7])*4
        Di9=int(ISBN[8])*3
        Di10=int(ISBN[9])*2

        sum=(Di1+Di2+Di3+Di4+Di5+Di6+Di7+Di8+Di9+Di10) 

        num=sum%11
        Di11=11-num 
        if Di11==10: 
            Di11='X'
        ISBNNumber=str(ISBN)+str(Di11) 
        print('The ISBN number is -->    ' + ISBNNumber) 

    else:
        print "Invalid input...\n" # In case the input is neither "1" or "2" for instance

quit() # executes once you've broken from the loop