2017-09-11 2 views
0

아주 기본적인 계좌 개혁 시스템을 만들려고합니다 제가 사용하는 코드는 200 달러의 비용을 지불하는 오브젝트를 사고 싶은지 사용자에게 묻습니다. 그 아이디어는 $ 200가 total이라는 변수에서 차감되지만, 어떤 이유로 그것이 작동하지 않는다는 것입니다."for"루프가 작동하지 않는 파이썬 수학 연산

if 문에 For 루프를 중첩 할 수 있는지 여부를 알 수 없습니다. 이것은 작동 시간

total = 1500 
print('Would you like to buy this item?') 
print('It costs 100 bucks') 
purchaseConfirm = input() 
if purchaseConfirm == 'yes': 
    for num in range(200): 
     total = total-1 # Why is this thing not functioning??? 
     print(total) 
     break 
+2

아무것도 차감하지 않거나 1을 뺀 것입니까? 왜냐하면'for' 내부의 코드는 한 번만 실행되기 때문입니다. 'break '를 제거하십시오 –

+1

왜 여기에'for' 루프가 필요합니까? 그냥 금액에서 직접 200을 공제하십시오. 또한 첫 번째 반복에서'for' 루프를 죽이는'break '가 있습니다 ... $ 1을 공제해야합니다. – roganjosh

+0

입력 내용은 무엇입니까? – AndyG

답변

2

주셔서 감사합니다 :

total = 1500 
print('Would you like to buy this item?') 
print('It costs 100 bucks') 
purchaseConfirm = raw_input() 
if purchaseConfirm == 'yes': 
    for num in range(200): 
     total = total-1 # Why is this thing not functioning??? 
     print(total) 

당신이 때까지 루프의 체류 break을 제거해야 ...

5

난 당신이 필요하다고 생각하지 않습니다 for 루프는 모두 ...

total = 1500 
print('Would you like to buy this item?') 
print('It costs 200 bucks') 
purchaseConfirm = input() 
cost = 200 
if purchaseConfirm == 'yes': 
    total = total - cost 
    print(total)