0

Why i run this code in "Land ho!" Code coach chellenge and it's wrong in case 3???(help me plz)

def timetowait(x): if x < 20: return 10 else: return 20 + timetowait(x-20) people=int(input()) print(timetowait(people)) #the mission is to calculate the time you come to the other side of the sea . taking people are in front of you as input , Each ride lasts 20 minutes, Including going back and forth, and each ride can carry 20 people #For example: Your input: 20 Output: 30 #because you are the 21st person and the first ride carry 20 people (not you) , so you will wait 20 for back and forth,then it takes you 10 minutes to go to other side

11th Jun 2021, 6:04 AM
TCorn
TCorn - avatar
2 ответов
+ 3
TCorn your solution is correct, but throw error (maximum recursion call) for big input: recursive function cannot be called successively more than 999 times (at least on sololearn python execution): https://code.sololearn.com/cGCKS7CT6wLk/?ref=app that means 998 times from itself... so, for input greater than (999*20 == 19 980), you will have error (no output) ^^ simple use of floor (whole) division (//) let you handle arbitrary big numbers: people = int(input()) print(10+(people//20)) (parenthesis inside expression -- around floor division -- are optional due to operator precedence)
11th Jun 2021, 10:30 AM
visph
visph - avatar
+ 2
I think the recursion is causing the problem. Why not just use a simple math expression? After 'else' you just want to return 10 plus 20 times the number of times 20 goes into x.
11th Jun 2021, 8:17 AM
David Ashton
David Ashton - avatar