+ 4
Kindly help me with the code to this python exercise. I am stuck somewhere....
A man is stuck at the bottom of a well. Each day, he climbs up 8 metres, and then at night, he slips downwards by 3 metres. Using loops(any loop of your choice), write a function to determine(and print!) how many days it takes for him to climb out of a well of any given depth, where the depth of the well is taken as input. E.G: f(17)=> the well is 17 metres deep. Day 1: climbs 8m, height=8m; slips 3m, height=8-3=5m; Day 2: climbs 8m, height=5+8=13m; slips 3m, height= 13-3=10m Day 3: climbs 8m, height=10+8=18m. But 18>17. STOP.(height climbed has exceeded well depth) Therefore, f(17)=3 days.
15 odpowiedzi
+ 9
You can do it with a for loop, but in my opinion this is a case where a while loop works better.
Use True as condition. Inside the loop:
- increment the day counter
- set depth to depth - climbs
- if depth is 0 or less break
- set depth to depth + slips
After the loop print the day counter.
+ 5
#too close.. but your height += 8 is outside loop so it won't update height then it's infinite loop..
depth = int(input())
height = 0
days = 0
while True:
days += 1
height += 8
if depth <= height:
break
else:
height -= 3
print(days)
+ 3
#Simplifying steps:
"""Step:1
E.G: f(17)=> the well is 17 metres deep. so
"""
deep = 17 #input
height = 0
Day = 0
"""Step2:
Day n: climbs 8m, height=8m; slips 3m, height=8-3=5m;"""
Repeat :
height += 8
day += 1
deep <= height : stop
else : height -= 3 ,
#Step3:
print(days)
Before attempting this, revise loop lessons.. How to create and use loops.. what is return, how the continue works..? You have incorrect understanding about these... Don't confuse your self more without revising... Hope it helps..
+ 3
Jayakrishna, Alright. I will check what you said.
+ 3
Ohh ohhh... You two, thanks so much for the help. I think i need to revise these loops all over again, and again, and again😪😣
+ 2
What have you tried?
You need a loop, a conditional, and a break statement.
+ 2
depth = int(input())
height = 0
days = 0
height += 8
if depth <= height:
break
else:
height -= 3
days += 1
print(days)
Here is what I can devise from your explanation... But then, the break function is giving an error. Says it's outside the loop. What could be the problem?
+ 2
depth = 17
height = 0
days = 0
height += 8
while True:
if depth <= height:
break
else:
height -= 3
days += 1
print(days)
That was a mistake. Look at this, the output is saying no output. Why is that so?
+ 2
days+=1 and height+=8
have to be inside the loop. Both before the if statement.
+ 1
Simon Sauter Yes, I have tried it....
depth = int(input())
climbs = 8
slips = 3
x = climbs - slips
days = 0
for i in depth:
if i > x:
days += 1
return x
x += climbs
continue
elif x > i:
print(days)
Here's my trial. I am sure I am missing out somewhere. Or probably my code is wrong
0
Share your try here..
You have clear steps to implement it.. What is your difficulty then?
0
Simon, can you kindly write your code to this question?
0
Try doing it yourself. Just follow the steps.
0
Where are you using a loop..?
Your code don't have a loop.. break is used inside the loop. Your if -else block should be added in a loop.. Add day+= 1 outside if -else block.
0
How can i complete my coursees?