2016-12-05 143 views
0
tries = 0 
while tries < 4: 
    deposit = input("Enter amount to deposit: ") 
    try: 
     deposit = int(deposit) 
     if deposit < 0: 
      raise "invalid output" 
    except: 
     print("This transaction cannot proceed. You entered amount in negative") 
     tries += 1 
    else: 
     print("Deposited: \tPKR ", deposit) 
     print("Current Balance: \tPKR %d"%(currentBalance + deposit)) 
     currentBalance += deposit 
     print("Would you like to do any more Transaction?") 
     answer = input("Enter Y for Yes and N for No: ") 
     try: 
      if answer.isalpha() == False: 
       raise "invalid output" 
     except: 
      print("Your input was considered as 'N'") 
      main() 
print("You entered invalid input three times. Now open your account again") 
main() 
+0

您是否在範圍試過'的嘗試(0,4):'呢? – doctorlove

+2

僅有代碼的問題很糟糕。 –

+0

歡迎來到Stack Overflow!我重新格式化了你的代碼:似乎有兩個相同代碼重疊的實例,所以我用一個實例替換了它。您應該嘗試解釋_why_您是否試圖將代碼重寫爲for循環,以及您試圖實現的最終目標是什麼。您可以嘗試評論中建議的一些想法,並嘗試編輯您的問題。祝你好運! – Praveen

回答

0
for tries in range(0, 4): 
    deposit = input("Enter amount to deposit: ") 
     try: 
      deposit = int(deposit) 
      if deposit < 0: 
       raise "invalid output" 
     except: 
      print("This transaction cannot proceed. You entered amount in negative") 
      tries += 1 
     else: 
      print("Deposited: \tPKR ", deposit) 
      print("Current Balance: \tPKR %d"%(currentBalance + deposit)) 
      currentBalance += deposit 
      print("Would you like to do any more Transaction?") 
      answer = input("Enter Y for Yes and N for No: ") 
      try: 
       if answer.isalpha() == False: 
        raise "invalid output" 
      except: 
       print("Your input was considered as 'N'") 
       main()