Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How will you improve my code? Gateux Programmer named Tim Posts: 10 Threads: 7 Joined: Jun 2019 Reputation: 0 Likes received: 0 #1 Jul-17-2019, 02:35 PM (This post was last modified: Jul-17-2019, 02:35 PM by Gateux. Edited 1 time in total.) I'm new to Python programming and right now i'm practicing past year assignment before school starts and my codes are fully functioning but it's in a big mess in terms of structure and there are certain limitations in my code e.g certain inputs can only accept uppercase, which I will highlight more at the end of this post. Here's the question: To code a program that calculates the maximum tenure of property loan The question is too long to put in text form on this post, so please read from the attached PDF. My Code: ``` def citizenship(): print("Enter whether at least one buyer is Singapore citizenship") citizen = str(input("Enter either Y for Yes or N for No: ")) if citizen == 'N': print("At least one buyer must be a Singapore citizen") else: numloan() def numloan(): loan = int(input("Enter number of HDB loans taken previously, valid values from 0: ")) if loan >= 2: print("Household status does not qualify for loan") else: lastproperty() def lastproperty(): print("Enter whether last owned property is a private residential property (local or overseas such as: \n - HUDC flat \n - Property acquired by gift \n Property inherited as a beneficiary under a will or as a result of the IntestateSuccession Act \n - Property owned/ acquired / disposed of through nominees") property = str(input("Enter either Y for Yes or N for No: ")) if property == 'Y': print("Household status does not qualify for loan") else: loanscheme() def main(): applicants = int(input("Enter number of applicants, valid values from 1 to 2: ")) citizenship() def loanscheme(): print("Enter which scheme loan is made under") scheme = str(input("Enter either F for Family or E for Extended Family or S for Single: ")) if scheme == 'F': family1 = float(input("Enter monthly income of first applicant, valid values from 0.0: ")) family2 = float(input("Enter monthly income of second applicant, valid values from 0.0: ")) children = int(input("Enter number of children, valid values from 1: ")) for i in range(children): childincome = float(input("Enter monthly income of children: ")) famincome = family1 + family2 + childincome if famincome > 12000: print("Not eligible. Average gross monthly household income exceeded") break if scheme == 'E': family3 = float(input("Enter monthly income of first applicant, valid values from 0.0: ")) family4 = float(input("Enter monthly income of second applicant, valid values from 0.0: ")) children = int(input("Enter number of children, valid values from 1: ")) print("Enter whether any child in household is married or applied HDB Fiance/Fiancee Scheme") marriedchild = str(input("Enter either Y for Yes or N for No: ")) if marriedchild == 'Y': marriedincome = float(input("Enter monthly income of child married or has applied HDB Fiance/Fiancee Scheme, valid values from 0.0: ")) fianceincome = float(input("Enter monthly income of fiance/fiancee/spouse of child, valid values from 0.0: ")) singleincome = float(input("Enter monthly income of any single child, valid values from 0.0: ")) elif marriedchild == 'N': for i in range(children): childincome1 = float(input("Enter monthly income of children: ")) famincome1 = family3 + family4 + childincome1 if famincome1 > 18000: print("Not eligible. Average gross monthly household income exceeded") break elif scheme == 'S': singleapplicant = float(input("Enter monthly income of single applicant, valid values from 0.0: ")) print("Enter whether you have owned or have disposed of any private residential property\nin the 30 months before the date of application for an HDB Loan Eligibility (HLE)\nletter.") print("A private residential property (local or overseas) will include:") print("- Property acquired by gift") print("- Property inherited as a beneficiary under a will or as a result of the\nIntestate Succession Act") print("- Property owned/ acquired/ disposed of through nominees") eligibility = str(input("Enter either Y for Yes or N for No: ")) if eligibility == 'Y': print("Not eligible. Must not own or have disposed of any private residential property in\n the 30 months and if own one business market/ hawker stall or\n commercial/ industrial property, must be operating there") stall = int(input("Enter number of market/ hawker stall or commercial/ industrial property you\n currently owned, valid values from 0: ")) if stall > 1: print("Not eligible. Must not own or have disposed of any private residential property in\n the 30 months and if own one business market/ hawker stall or\n commercial/ industrial property, must be operating there") print("Enter whether operating the business there, and have no other sources of income") incomesource = str(input("Enter either Y for Yes or N for No: ")) if incomesource == 'N': print("Not eligible. Must not own or have disposed of any private residential property in\n the 30 months and if own one business market/ hawker stall or\n commercial/ industrial property, must be operating there") age1 = int(input("Enter age of first applicant, valid values from 21.0: ")) age2 = int(input("Enter age of second applicant, valid values from 21.0: ")) lease = int(input("Enter number of years of remaining lease, valid values from 1 to 99: ")) agetotal = age1 + age2 ageavg = agetotal // 2 leasecount = ageavg + lease if leasecount < 80: print("Not eligible. Remaining Lease of HDB Flat is insufficient") if lease < 20: print("Not eligible. Remaining Lease of HDB Flat is insufficient") maxloan = 65 - ageavg maxloan1 = lease - 20 if maxloan < 25 and maxloan1: print("Maximum years of loan tenure is", maxloan) else: print("Maximum years of loan tenure is", maxloan1) ```Even though my codes are fully functioning, it didn't really meet the objective 100%. Firstly i need guidance on how can I improve this messy structure because the assignment specifically states that we cannot use global variables for this program so i ended up coding is this way by calling another function from within an function. As I mention in the start of this post, i coded all these purely base on self-study as my school have yet to start and it's my first exposure to python, so pardon the mess as I'm trying to learn. Here's the problem i encounter: 1. Accept inputs in both upper and lower case - I can't figure how to do this, as right now my code only accept upper case e.g "Y" ```print("Enter whether you have owned or have disposed of any private residential property\nin the 30 months before the date of application for an HDB Loan Eligibility (HLE)\nletter.") print("A private residential property (local or overseas) will include:") print("- Property acquired by gift") print("- Property inherited as a beneficiary under a will or as a result of the\nIntestate Succession Act") print("- Property owned/ acquired/ disposed of through nominees") eligibility = str(input("Enter either Y for Yes or N for No: ")) if eligibility == 'Y': print("Not eligible. Must not own or have disposed of any private residential property in\n the 30 months and if own one business market/ hawker stall or\n commercial/ industrial property, must be operating there")```2. Over here i want the program to stop asking for anymore inputs since i printed out not eligible. But i notice even after the error message is printed, the next line of input continue to prompt even though i want it to end there, can't figure how to fix this. In fact this happens for some other parts in my code also. ```age1 = int(input("Enter age of first applicant, valid values from 21.0: ")) age2 = int(input("Enter age of second applicant, valid values from 21.0: ")) lease = int(input("Enter number of years of remaining lease, valid values from 1 to 99: ")) agetotal = age1 + age2 ageavg = agetotal // 2 leasecount = ageavg + lease if leasecount < 80: print("Not eligible. Remaining Lease of HDB Flat is insufficient") if lease < 20: print("Not eligible. Remaining Lease of HDB Flat is insufficient") ```3. Over here i have created some variables to do calculation to determine if the user is eligible for the property loan but this is only when there is 2 applicants, i can't figure how to fit in one more calculation for single applicant which should go like this: ```singleage = int(input("Enter age of applicant, valid values from 21.0: ")) loantenure = 65 - singleage loantenure2 = lease - 20 ```4. If the user enter the value e.g 4 for number of children, the program suppose to prompt "Enter monthly income of child 1, valid values from 0.0:" "Enter monthly income of child 2, valid values from 0.0:" "Enter monthly income of child 3, valid values from 0.0:" "Enter monthly income of child 4, valid values from 0.0:" But i can't figure out how to automatically display the number e.g child 2 Initially i thought of using lens but for integer lens can't be used The next thing that comes to my mind is for loop but i just don't know how to write this for this purpose 5. For prompting the age of applicant, right now i have two prompts for applicant 1 and applicant 2 but i need to only show 1 prompt if it's a single applicant, again i can't figure out how to achieve this 6. Lastly for this code here: ```maxloan = 65 - ageavg maxloan1 = lease - 20 if maxloan < 25 and maxloan1: print("Maximum years of loan tenure is", maxloan) else: print("Maximum years of loan tenure is", maxloan1) ```Basically what i'm trying to achieve here is print out the value that is the shortest of 25 years but right now it seems no matter what, the value of maxloan keeps getting displayed only. Attached Files   Assignment.pdf (Size: 65.97 KB / Downloads: 28) Yoriz Posts: 1,117 Threads: 28 Joined: Sep 2016 Reputation: 92 Likes received: 250 #2 Jul-17-2019, 06:24 PM (This post was last modified: Jul-17-2019, 06:27 PM by Yoriz. Edited 1 time in total.) (Jul-17-2019, 02:35 PM)Gateux Wrote: 1. Accept inputs in both upper and lower case - I can't figure how to do this, as right now my code only accept upper case e.g "Y The string method `lower` can be used to make the comparison always be between lower case strings. ```users_input = 'Y' if users_input.lower() == 'y': print('user input Yes')`````````Output: user input Yes``````Note: there is also a `upper` string method if the code was to compare upper strings. metulburr The Castle of aaarrrrggh Posts: 4,468 Threads: 284 Joined: Sep 2016 Reputation: 133 Likes received: 1151 #3 Jul-17-2019, 06:26 PM (Jul-17-2019, 02:35 PM)Gateux Wrote: 1. Accept inputs in both upper and lower case 1 and 2 This is explained in our validating input tutorial Your questions combined look a little overwhelming to respond to. You have permission to split your post up into multiple threads. It would be more likely to get answers that way. Recommended Tutorials: BBcode, Forum Rules and Instructions, How to ask smart questions, the Basics, Classes, Python Gotchas Yoriz Posts: 1,117 Threads: 28 Joined: Sep 2016 Reputation: 92 Likes received: 250 #4 Jul-17-2019, 06:50 PM (This post was last modified: Jul-17-2019, 07:07 PM by Yoriz. Edited 2 times in total.) (Jul-17-2019, 02:35 PM)Gateux Wrote: 3. Over here i have created some variables to do calculation to determine if the user is eligible for the property loan but this is only when there is 2 applicants, i can't figure how to fit in one more calculation for single applicant which should go like this: ```singleage = int(input("Enter age of applicant, valid values from 21.0: ")) loantenure = 65 - singleage loantenure2 = lease - 20 ``` I don't understand this question (Jul-17-2019, 02:35 PM)Gateux Wrote: 4. If the user enter the value e.g 4 for number of children, the program suppose to prompt "Enter monthly income of child 1, valid values from 0.0:" "Enter monthly income of child 2, valid values from 0.0:" "Enter monthly income of child 3, valid values from 0.0:" "Enter monthly income of child 4, valid values from 0.0:" But i can't figure out how to automatically display the number e.g child 2 Initially i thought of using lens but for integer lens can't be used The next thing that comes to my mind is for loop but i just don't know how to write this for this purpose Use string formatting to add the looped value to the string, you would also need to append the results to a list or you would only have the last entered value stored in a single variable. ```no_of_children = 2 childrens_monthly_income = [] for child_no in range(no_of_children): income = input( f'Enter monthly income of child {child_no}, valid values from 0.0:') childrens_monthly_income.append(income) print(childrens_monthly_income)`````````Output: Enter monthly income of child 0, valid values from 0.0:250 Enter monthly income of child 1, valid values from 0.0:500 ['250', '500']`````` (Jul-17-2019, 02:35 PM)Gateux Wrote: 5. For prompting the age of applicant, right now i have two prompts for applicant 1 and applicant 2 but i need to only show 1 prompt if it's a single applicant, again i can't figure out how to achieve this Use an `if` that checks for the need to input the age of a 2nd applicant and put the prompt inside the if statement (Jul-17-2019, 02:35 PM)Gateux Wrote: 6. Lastly for this code here: ```maxloan = 65 - ageavg maxloan1 = lease - 20 if maxloan < 25 and maxloan1: print("Maximum years of loan tenure is", maxloan) else: print("Maximum years of loan tenure is", maxloan1) ```Basically what i'm trying to achieve here is print out the value that is the shortest of 25 years but right now it seems no matter what, the value of maxloan keeps getting displayed only. maxloan1 has no condition applied to it the `if` statement is checking if maxloan is less than 25 and maxloan1 evaluates to `True` ndc85430 Lumberjack Posts: 104 Threads: 0 Joined: Apr 2017 Reputation: 5 Likes received: 18 #5 Jul-20-2019, 12:55 PM You seem to be passing the return value of `input` to `str` in several places (e.g. lines 3 and 20 in the code in the first post). That's unnecessary, since `input` already returns a string. The code is also quite dense, particularly the `loanscheme` function.It would be more readable if it were split into smaller functions each doing one specific thing that were then called from `loanscheme`. Also, adding blank lines between sections of code even within a function helps readability, much like how you'd use paragraphs in writing to split up different thoughts, etc. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Improve this code (Receipt alike) Leonzxd 10 1,621 Jun-26-2018, 03:33 PM Last Post: Leonzxd

Forum Jump:

Users browsing this thread: 1 Guest(s)