Python Forum
Can someone please help me write the code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone please help me write the code
#1
Hei, I am beginer in python. Can someone please help me write the code for below.
There are 150 apples on a tree. They are increased by 15% every week.
how many number of apples will we get after 5 weeks.
thank you.
Reply
#2
We will not do your homework for you.
Please make an effort, show what you have for code, and then ask specific questions about areas where you are having a problem.
Reply
#3
First you should try how you would do this if you had no computer.
You would increment the number by multiplying the number of apples with 1.15. You would repeat this 5 times. Then you would have the answer. This is the algorithm.
Now write this in Python code.
Reply
#4
I had written this code, But it is showing for 1 week, How can I multiply this to get it for 5 weeks.
apples=150
increase_per_week=apples/100*15
per_one_week=apples+increase_per_week
print(per_one_week)

Answer:172.5
buran write Feb-07-2021, 01:33 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#5
If I understood it right, you just have to repeat what you have done there five times.

Hint: Maybe using a loop would help
Reply
#6
Be smart with you calculations. Do you need to know the increase per week or do you just need to know the total after 5 weeks?

start = 150
week 1 = start * 1.15
week 2 = week 1 * 1.15
week 3 = week 2 * 1.15
week 4 = week 3 * 1.15
week 5 = week 4 * 1.15

Or

week 5 = 150 * 1.15 * 1.15 * 1.15 * 1.15 * 1.15
week 5 = 150 * 1.15^5

You may want to correct the result to eliminate partial apples.

This can be made to work for the general case when you specify the starting value, the increase rate and the number of increase periods.

The first step in writing code is solving the problem. Once you solve the problem it is fairly simple to translate the solution to code.
Reply
#7
(Feb-07-2021, 04:04 PM)deanhystad Wrote: week 5 = 150 * 1.15^5
Haha @deanhystad, that is indeed the most intelligent way to solve the problem.
But I believe Ram needs to learn how to repeat blocks of code.
@Ram: did you already learned about range()? Like this:
for i in range(5):
    apples = apples * 1.15
If you have not then you should do something with the while statement.
Reply
#8
You should solve the problem as described in the statement of work. The statement of work says:
Quote:There are 150 apples on a tree. They are increased by 15% every week.
how many number of apples will we get after 5 weeks.
This could be done with a loop, but that is not a good choice for solving this problem. You wouldn't do this would you?
# Calculate 5 squared
result = 1
for _ in range(5):
    result = result * 5
Just because you are writing programs for a programming class it doesn't mean you should turn off you brain. If I was teaching the course I would fail everyone who used a loop for this assignment.
Reply
#9
I would argue that the intended solution depends on the full exercise text and the current focus in the course at the time the exercise was given and we have neither of them. It would be nice to learn about both before the final judgement...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a dict code in python ariellea88 4 1,014 Oct-31-2023, 08:45 AM
Last Post: buran
  how to write code demon_calcifer 1 1,626 Nov-16-2021, 04:09 PM
Last Post: Larz60+
  Please help me to write code for this vij 8 3,795 Jun-10-2020, 12:13 PM
Last Post: pyzyx3qwerty
  Write pseudo code for a class assignment Scrimshot 3 3,402 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Write a code to output in alphabetical order AbdelaliPython 1 4,627 Jan-19-2018, 09:03 PM
Last Post: j.crater
  how to write a code for term that can't begin or end with a stopword desul 3 3,793 Mar-18-2017, 03:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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