2014-02-16 110 views
2

這是我的時刻代碼:需要蟒蛇幫助

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) 

我希望它循環,所以當它給我的11位數字,當我能選擇1我希望它循環回菜單詢問我是否要輸入10位數字或退出。 不應該太難,但我花了太長時間才找到解決辦法。

感謝

+1

有什麼錯誤? – connor

+0

你不能只說'退出'。 –

+0

嘗試退出()如果它是第二個選項 – connor

回答

0

替換此代碼:

if decision == "2": 
    quit 

有了這個:

if decision == "2": 
    quit() 

你被quit後忘記括號。

+0

我不能相信我錯過了(FACEPALM) – user3316508

+0

請修復縮進 –

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