2013-10-19 50 views
-1

這是我重複序列時使用的代碼,但它似乎沒有工作,任何人都可以看到任何問題?代碼是貨幣轉換器。即時通訊使用Python 3.3爲什麼不重複這段代碼? Python 3.3

userDoAgain = input("Would you like to use again? (Yes/No)\n") 
if userDoAgain == "Yes": 
     getChoice() 
elif userDoAgain == "No": 
     print("Thankyou for using this program, Scripted by PixelPuppet") 
     import time 
     time.sleep(3) 
else: 
     print("Error: You entered invalid information.") 
     doagain() 

編輯,這是其餘代碼:

if userChoice == "1": 
userUSD = float(input("Enter the amount of USD you wish to convert.\n")) 


UK = userUSD * 0.62 

print("USD", userUSD, "= ", UK, "UK") 





elif userChoice == "2": 
    UK = float(input("Enter the amount of UK Currency you wish to convert.\n")) 

    userUSD = UK * 1.62 

    print("UK", UK, "= ", userUSD, "USD") 


    def doagain(): 

     userDoAgain = raw_input("Would you like to use again? (Yes/No)\n") 
    if userDoAgain == "Yes": 
      getChoice() 
    elif userDoAgain == "No": 
      print("Thankyou for using this program, Scripted by PixelPuppet") 
      import time 
      time.sleep(3) 
    else: 
      print("Error: You entered invalid information.") 
      doagain() 
+1

你可以發佈你的代碼的其餘部分?函數'doagain'的'def'語句在哪裏? – Brionius

+1

爲什麼可疑的「輸入時間」,如果你在那個文件中需要它,就在代碼頂部執行它。 – Leonardo

+2

這個縮進是否正確? – tacaswell

回答

2

一般來說,使用遞歸來處理Python重複控制流程是一個好主意。它更容易,而且使用循環代替問題更少。因此,我們建議您使用while循環,而不是定義功能doagain以確保您能夠得到有關再次運行問題的答案。對於您要重複的更大功能,我建議使用循環。

def repeat_stuff(): 
    while True: # keep looping until told otherwise 

     # do the actual stuff you want to do here, e.g. converting currencies 
     do_stuff_once() 

     while True: # ask about doing it again until we understand the answer 
      userDoAgain = input("Would you like to use again? (Yes/No)\n") 
      if userDoAgain.lower() == "yes": 
       break    # go back to the outer loop 
      elif userDoAgain.lower() == "no": 
       print("Thank you for using this program") 
       return    # exit the function 
      else: 
       print("Error: You entered invalid information.") 

請注意,我已經改變了yes/no輸入字符串的檢查,以區分insenstive,這是去一個較爲用戶友好的方式。

-1

也許你需要使用的功能「的raw_input」,而不是唯一的輸入。

+0

在Python 3中沒有'raw_input'。 – zero323

+1

您如何知道他在使用Python 2而不是3? –

+1

雖然沒有_definite_證明,但他的'print'語法有點指出他在Python 3中的事實。 – iCodez

0

您正在使用遞歸(該函數調用自己),而只是將要在while循環中重複的代碼包裝起來會更好。這種用法的

實施例:

userContinue = "yes" 

while (userContinue == "yes"): 
    userInput = input("Type something: ") 
    print("You typed in", userInput) 
    userContinue = input("Type in something else? (yes/no): ").lower()