2016-09-21 5 views
0

사용자가 가격을 입력 한 후 프로그램이 입력을 기반으로 배송비를 출력하는 작은 프로그램을 작성하고 있습니다.사용자 입력에 따라 오른쪽 else 문 선택

#Initilaize variables 

lowUS = 6.00; 
medUS = 9.00; 
highUS = 12.00; 
lowCan = 8.00; 
medCan = 12.00; 
highCan = 15.00; 

#Greet user and ask for input 
print("Welcome to Ben's shipping calculator!"); 
print("We will calculate your shipping cost for you!"); 


orderTotal = float(input("Please input your total amount.")); 
country = input("In which country do you reside? Please type C for Canada or U or USA. "); 

#Validate input 
while country not in {'u', 'c', 'C', 'U'}: 
    print ("Invalid input. Please try again. ") 
    country = input("In which country do you reside? Please type C for Canada or U or USA. "); 

#Determine how much the shipping fee is 
if country == 'U' or country == 'u': 
    if orderTotal <= 50.00: 
     if orderTotal > 50.00 and orderTotal <= 100.00: 
      if orderTotal > 100.00 and orderTotal <= 150.00: 
       if orderTotal > 150.00: 
        print("Your shipping is free and your grand total is", orderTotal) 
       else: 
        print("Your shipping fee is: ", highUS); 
        orderTotal = (orderTotal + highUS); 
      else: 
       print("Your shipping fee is: ", medUS); 
       orderTotal = (orderTotal + medUS); 
     else: 
      print("Your shipping fee is: ", lowUS); 
      orderTotal = (orderTotal + lowUS); 

elif country == 'c' or country == 'C': 
    if orderTotal <= 50.00: 
     if orderTotal > 50.00 and orderTotal <= 100.00: 
      if orderTotal > 100.00 and orderTotal <= 150.00: 
       if orderTotal > 150.00: 
        print("Your shipping is free and your grand total is", orderTotal) 
       else: 
        print("Your shipping fee is: ", highCan); 
        orderTotal = (orderTotal + highCan); 
      else: 
       print("Your shipping fee is: ", medCan); 
       orderTotal = (orderTotal + medCan); 
     else: 
      print("Your shipping fee is: ", lowCan); 
      orderTotal = (orderTotal + lowCan); 

print("Your grand total is: $", orderTotal); 

나는 파이썬 프로그래밍에 아주 새로운 오전 나는이 그것을 갈 수있는 좋은 방법입니다 있는지 확실하지 않습니다,하지만 나는 그것을 시도 줄 것이라고 생각 때문에 나는 경우 - 다른 진술을 배우고 . 지금까지는 금액에 "50"을 입력 한 경우에만 작동합니다. 그것은 "50"에 대해서만 국가 기준으로 계산합니다. 나는 누군가가 도울 수 있고 설명 할 수 있다면 내가 잘못한 곳인지 잘 모르겠다.

+0

첫 번째'if'는 50보다 작거나 같은 경우에만 입력됩니다. 다음 번 확인은 50보다 큰 경우입니다. 그러면 블록이 실행되지 않습니다. 'else' 절은 실행할 수있는 유일한 것입니다 ... –

+0

파이썬은 개행 문자 외에 줄 종결 자도 없습니다. 세미콜론은 완전히 불필요합니다. 이것은 C가 아닙니다! (또는 자바, 또는 자바 스크립트, 또는 다른 무엇이든지) – MattDMo

답변

1

첫 번째 if은 50보다 작거나 같은 경우에만 입력됩니다. 다음 번 확인은 50보다 크면 불가능하므로 블록이 실행되지 않습니다 ... 그래서 else 절은 실행할 수있는 유일한 것입니다. 기본적으로 중첩 된 if 문은 이미 수행 할 기준이 포함 된 if에서 제외되어 실행되지 않습니다. 당신은 귀하의 논리 구조 조정 오프 최고야

:

if orderTotal > 150: 
    # do something 
elif orderTotal > 100: 
    # do something 
elif orderTotal > 50: 
    # do something 
else: # 50 or under... 
    # do something else 
+0

그래, 나는 실수를 지금 본다, 나는 실제로 그것을 이렇게하는 것에 대해 생각하고 있었지만 그것이 정확했을 지 100 % 확실하지 않았다. 고맙습니다! – legendaryxv2

0

당신의 논리는 조금 별난입니다.

if orderTotal <= 50.00: 
    if orderTotal > 50.00 and orderTotal <= 100.00: 
     # ** this will never run ** 
    else: 
     # This will run, if orderTotal <= 50.00 

아무것도 경우 orderTotal > 50.00을 일어나지 않을 것이라고 대한 else이 없기 때문에 : 양이 < = $ 50면, 그것은 "경우"를 실행하지 않습니다 중첩 된 것을 다음하지> $ 50 < = $ 100 그래서 아무거나 if orderTotal <= 50.00 테스트. @ 존 클레멘트의 대답은 코드를 구조화하는 올바른 방법을 보여줍니다.

관련 문제