2014-03-02 9 views
-1

안녕하세요 저는 파이썬을 배우고 있으며 돈을 달러, 유로 또는 영국 파운드로 변환하는 작은 프로그램을 만들고자합니다. 어떤 사람이 나를 도우면서 왜 일하지 않는지 말해 줄 수 있습니까? 감사합니다 !!!파이썬 프로그램이 작동하지 않습니다. 입력 == 문자열이 작동하지 않습니다.

def calculate(): 
    currency_input = input("Insert value:") 
    dollar = 34 
    euro = 36 
    pound = 52 
    select_currency = input("Insert currency(dollar,euro or pound):") 
    if select_currency is "dollar": 
     currency_input * dollar 
    elif select_currency is "euro": 
     currency_input * euro 
    elif select_currency is "pound": 
     currency_input * pound 
    else: 
     print ("Please select a currency(dollar,euro,pound)!") 
    calculate() 
calculate() 

답변

0

사용한다

당신은 모든 문자열 테스트를 해결하고, 또한 실제로 계산의 결과를 저장해야 is 대신 ==이 표시됩니다. is은이 경우에 수행한다고 생각하지 않습니다. 그것에 대한 자세한 내용은 here입니다.

을 사용하면 사용자가 Dollars을 입력해도 성공할 수 있습니다.

사용자가 잘못된 정보를 입력 한 경우 처리 할 수 ​​있기를 원합니다. 당신은 확실히 사용자가 정확한 입력을위한 사용자 요구에 계속 루프 while Truecurrency_input

사용하기 위해 숫자 만 입력 할 수있는 tryexcept 블록을 사용한다. 그들이 정확한 입력을 입력하면 break 성명서로 질문하는 것을 멈 춥니 다.

사전을 사용하면 통화 이름과 관련 값을 쉽게 저장할 수 있습니다.

또한 수학은 모든 통화에 대해 매우 동일합니다. 변경 사항은 통화 (달러, 유로 ...)의 가치이기 때문에 사용자가 선택한 것을 검색하고 그 시간을 곱하기 만 할 수 있습니다 the currency_input

def calculate(): 
    # we only want the user to input numbers 
    while True: 
     try: 
      currency_input = float(input('Insert value: ')) # input always returns a str, we need to type cast 
      break # if input is valid we break out of the loop and move on 
     except TypeError: # handle the error when the input is not a number 
      print('Please enter a number.') 

    # use a dictionary because it is easier to read 
    currency_dict = { 
     'dollar': 34, 
     'euro': 36, 
     'pound': 52} 

    # get the type of currency and do the math 
    while True: 
     select_currency = input('Insert currency(dollar,euro or pound): ').lower() 
     if select_currency not in currency_dict: # if the users enter something that is not in the dict 
      print('Invalid currency') # oops, try again 
     else: 
      money = currency_input * currency_dict[select_currency] # we do the math 
      return money # return allows us to further manipulate that variable if we so desire 

print(calculate()) 

두 가지 개선 사항을 지적 해 주신 Martijn Pieters에게 감사드립니다.

0

평등이 아닌 신원을 테스트하고 있습니다. 대신 == 사용하여 이름 select_currency이 같은 객체를 참조하면

if select_currency == "dollar": 

is 테스트를; 두 개의 객체는 별개이지만 여전히 동일한 값을 가지고 있습니다.이 객체는 ==으로 테스트합니다.

if select_currency == "dollar": 
    result = currency_input * dollar 
elif select_currency == "euro": 
    result = currency_input * euro 
elif select_currency == "pound": 
    result = currency_input * pound 

쉬운 아직 여기 사전을 사용하는 것입니다 :

currencies = { 
    'dollar': 34, 
    'euro': 36, 
    'pound': 52, 
} 
if select_currency in currencies: 
    result = currency_input * currencies[select_currency] 
else: 
    print ("Please select a currency(dollar,euro,pound)!") 
+0

이것은 작동하지만 유로 또는 달러 또는 파운드를 입력하면 곧바로 진행됩니다. 왜 그럴까요? 나는 소문자 "달러"... 또는 유로 또는 어떤 선택으로 정확하게 타자를 쳤다 ...? – Victor

+0

'print (repr (select_currency))'출력은 무엇입니까? –

관련 문제