2014-11-18 1 views
1

ATM 코드를 작성 중입니다. 최선의 노력에도 불구하고 수정할 수없는 코드가 있습니다. 다음은 오류 메시지입니다 :파이썬 - 밑이 10 인 int()에 대한 리터럴이 잘못되었습니다.

line 35, in Withdraw 
back2_menu = int(input("Do You Wish To Have Another Transation? ")) 
ValueError: invalid literal for int() with base 10: 'Y' 

그리고 여기이 나의 프로그램이 원활하게 실행 난 그냥 해결하는 방법 궁금 코드

def Withdraw2(self): 
    amount = int(input("How Much Do You Wish To Withdraw: \n £")) 
    if int(amount) <= self.balance: 
     self.balance = self.balance - amount 
     print("Withdrawl Accepted. \nYour New Balance Is: £" + str(self.balance)) 
    else: 
     print("Withdrawl Denied. \n You Have £" + str(self.balance) + "Within Your Account") 
     again = int(input("Do You Wish To Enter Another Amount")) 
     if again in ("Y", "y", "Ye", "ye", "Yes", "yes"): 
      print(atm.Withdraw2()) 
     else: 
      backmenu = str(input("Do You Wish To Have Another Transation? ")) 
      if backmenu in ("Y", "y", "Ye", "ye", "Yes", "yes"): 
       print(atm.Menu()) 
      else: 
       print(atm.End()) 

의 번잡 한 작품이다. 감사합니다.

+3

int ("Y")'는 무엇입니까? –

+0

문자열을 int로 변환하려고합니다. 'int' 캐스트를 제거하면 괜찮을 것입니다. 'back2_menu = input ("당신은 다른 변환을 원합니까?")'. 이 코드를 코드에 적용하면이 오류가 몇 군데 표시됩니다. – asdf

+0

@BadKarma, 많은 감사합니다. 이 코드는 완벽하게 작동합니다. –

답변

1

int (...) 함수는 문자열 정수 (즉, "123")를 따옴표없는 정수 정수 (즉 123)로 변환합니다.

"y"는 정수 구문을 따르지 않으므로 int ("y")는 유효하지 않습니다.

대신에 :

again = int(input("Do You Wish To Enter Another Amount")) 

추가로 몇 줄과 같은 작업을 수행하고 말 :

again = str(input("Do You Wish To Enter Another Amount")) 

그런 다음 당신은을 구문 분석하는 시도로 인해 발생하는 오류를받지 않습니다 'Y'의 정수. 당신이 번호를 입력하면

amount = int(input("How Much Do You Wish To Withdraw: \n £")) 

, 유효합니다 이전 입력

참고. 그러나 int (3.4)는 3으로 자릅니다.

+0

입력은 기본적으로 문자열을 반환하기 때문에 입력을'str'으로 변환 할 필요가 없습니다 – asdf

관련 문제