Python Forum
Guidance on solving an "if else loop" in this small project - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Guidance on solving an "if else loop" in this small project (/thread-25331.html)



Guidance on solving an "if else loop" in this small project - ando - Mar-27-2020

Hi all,

Thank you in advance for giving this a read. I have completed 99.9% of this assignment.
The goal of the program is to ask user to enter 'Car purchased price' and how many loan term years (only two options 2or3 years). The only error I get is when the user inputs the wrong loan term

Error:
Traceback (most recent call last): File "*****/****/****/****/HW#2Q1.py", line 49, in <module> endingB = initialBalance - installment NameError: name 'installment' is not defined
My goal is to have a loop or have the program end if the user enters the wrong loan term year.

The 'If else' syntax works until the user enters the wrong loan term year. My assumption is that the program keeps executing therefore I get this traceback error.

I'm using IDLE 3.8.1. Also, there is a definitely a different approach to this code but we are constrained to what we've learned in chp1-chp5 in class.

#CONSTANTS
APR=0.06
DOWN_PAYMENT=0.10
MONTH=1
YEAR=1

#input from user
purchasePrice=float(input('Enter Car Purchased Price:$'))
loanTerm=int(input('Enter Loan Terms (2 or 3 yrs):'))


#headings
titleMonth='Month'
titleYear='Year'
titleIB='Starting Balance'
titleI='Installment'
titleEB='Ending Balance'
titleRIM='Remaining Installments'

#downpayment calculation
downPayment=purchasePrice * DOWN_PAYMENT


#initialbalance calculation    
initialBalance=(purchasePrice - downPayment) + ((purchasePrice - downPayment)*APR*(loanTerm))


#installment payments
if loanTerm == 2 :
    installment=(initialBalance/24)
        
elif loanTerm==3:
      installment=(initialBalance/36)

else:
    print('Error: Please enter correct Loan Term:=')
  
#installmentleft

resultIM = int(loanTerm* 12)

#printing Headers
print('%-20s %-10s %-20s %-20s %-20s %s' %(titleMonth,titleYear,titleIB,titleI,titleEB,titleRIM))

#while loop calculation
while MONTH <=  (loanTerm * 12):
    endingB = initialBalance - installment
    
    print('%-20s %-10s %-20s %-20s %-20s %s' %(MONTH,YEAR,initialBalance,installment,endingB,resultIM))
    initialBalance=endingB

    YEAR= int(MONTH/12)+1
    resultIM = resultIM - 1 
    MONTH = MONTH+1
    
    



RE: Guidance on solving an "if else loop" in this small project - deanhystad - Mar-27-2020

When the entered loan term is invalid the program prints a message. Is printing supposed to stop the program?


RE: Guidance on solving an "if else loop" in this small project - perfringo - Mar-27-2020

As you are using 3.8 then you can just do:

>>> while (ham := int(input('Enter # of hams: '))) not in (1, 2):
...     print('Please enter 1 or 2!')
... 
Enter # of hams: 5
Please enter 1 or 2!
Enter # of hams: 2
>>> ham
2
Note that program will blow up if user enters something which is not convertible to int. Therefore recommended approach would be using try..except.


RE: Guidance on solving an "if else loop" in this small project - ando - Mar-27-2020

(Mar-27-2020, 05:09 AM)deanhystad Wrote: When the entered loan term is invalid the program prints a message. Is printing supposed to stop the program?

I'm new to python.So what I've experience to this point is having the program stop when it reaches the 'else:' part of the loop. I could be wrong. Please someone correct me and clarify this for me.

[quote='perfringo' pid='108494' dateline='1585298928']
As you are using 3.8 then you can just do:

>>> while (ham := int(input('Enter # of hams: '))) not in (1, 2):
...     print('Please enter 1 or 2!')
... 
Enter # of hams: 5
Please enter 1 or 2!
Enter # of hams: 2
>>> ham
2
Beautiful beautiful perfringo! This put the cherry ontop. Okay.. for my knowledge. The not in is checking if the user input is true with 1 or 2, if it's false then it goes to print: Print Enter 1 or 2

I just looked up ':=' walrus operator and this makes it possible to write a true statement within the user input? Am I understanding this correctly?

Thanks again for your input!


RE: Guidance on solving an "if else loop" in this small project - deanhystad - Mar-27-2020

You are wrong about else. if, elif, and else have nothing to do with ending programs. They do exactly what they do in natural language.
import sys

a = int(input("Enter a number "))

print("Will see this if the user entered a number")

if a == 5:
    print("To see this a must equal 5")
elif a < 7:
    print("To see this a must be less than 7 and cannot equal 5")
else:
    print("To see this a is not equal to 5 and not less than 7")

print("This is outside the if statement, so it prints every time")

if a == 6:
    sys.exit(0)

print("This gets printed if a is not 6")
Output:
Enter a number 6 Will see this if the user entered a number To see this a must be less than 7 and cannot equal 5 This is outside the if statement, so it prints every time >>> =========== RESTART: C:\Users\djhys\Documents\Python\Musings\junk.py =========== Enter a number 5 Will see this if the user entered a number To see this a must equal 5 This is outside the if statement, so it prints every time This gets printed if a is not 6 >>> =========== RESTART: C:\Users\djhys\Documents\Python\Musings\junk.py =========== Enter a number m Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 3, in <module> a = int(input("Enter a number ")) ValueError: invalid literal for int() with base 10: 'm'
The if statement cannot end the program, nor can the else. All they can do is control what code gets run.

The walrus operator lets you do assignment inside a comparison.
# With walrus
if (x := input('Type Hello: ')) != 'Hello':
    print(x, 'is not Hello')

# Without walrus
x = input('Type Hello: ')
if x != 'Hello':
    print(x, 'is not Hello')

# Is a syntax error
if (x = input('Type Hello: ')) != 'Hello':
    print(x, 'is not Hello')



RE: Guidance on solving an "if else loop" in this small project - ando - Mar-27-2020

I see I see... thank you for clarifying!

I just submitted my program!