Python Forum
Guidance on solving an "if else loop" in this small project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guidance on solving an "if else loop" in this small project
#1
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
    
    
Reply
#2
When the entered loan term is invalid the program prints a message. Is printing supposed to stop the program?
Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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!
Reply
#5
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')
Reply
#6
I see I see... thank you for clarifying!

I just submitted my program!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need guidance on String indexing Gateux 3 2,119 Jun-22-2019, 12:44 PM
Last Post: perfringo
  Small game (school project) Ganesh 7 5,589 Nov-08-2017, 09:04 PM
Last Post: Ganesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020