Python Forum
How will you improve my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How will you improve my code?
#4
(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 Think



(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
Reply


Messages In This Thread
How will you improve my code? - by Gateux - Jul-17-2019, 02:35 PM
RE: How will you improve my code? - by Yoriz - Jul-17-2019, 06:24 PM
RE: How will you improve my code? - by metulburr - Jul-17-2019, 06:26 PM
RE: How will you improve my code? - by Yoriz - Jul-17-2019, 06:50 PM
RE: How will you improve my code? - by ndc85430 - Jul-20-2019, 12:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I improve string similarity in my current code? SUGSKY 3 2,380 May-28-2020, 05:16 AM
Last Post: deanhystad
  Improve this code (Receipt alike) Leonzxd 10 7,935 Jun-26-2018, 03:33 PM
Last Post: Leonzxd

Forum Jump:

User Panel Messages

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