简体   繁体   English

需要ISBN计算器的python帮助

[英]Need python help for ISBN calculator

Here is my code at the moment: 这是我目前的代码:

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) 

I want it to loop so when it gives me the 11 digit number when I choose choice 1 I want it to loop back to the menu asking me whether I want to enter a 10 digit number or quit. 我希望它循环播放,因此当我选择选项1时,当它给我11位数字时,我希望它循环回到菜单,询问我是否要输入10位数字或退出。 Shouldn't be too hard but I have spent way too long at it and just cant find a fix. 不应该太辛苦,但是我花了太长时间,无法找到解决方法。

Thanks 谢谢

Replace this code: 替换此代码:

if decision == "2": 
    quit

With this: 有了这个:

if decision == "2": 
    quit() 

You were forgetting the parentheses after quit . quit后你忘记了括号。

Try wrapping the whole thing in a while loop like so: 尝试将整个内容包装在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM