Python Forum
creating functions - make optional argument a reversal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating functions - make optional argument a reversal
#1
I've been stuck on this for a cumulative 10 hours now. I have to call a function from a main function; in that secondary function I have to return a range to the main function, and the main function then prints the output. The main function then needs to refer to the secondary function again but this time return a reversed list to the main so that a reversed list gets printed as output. My problem is that I cannot figure out how to set an optional argument to a reversal. I know the code below is horribly wrong, but I have changed it at least 50 times trying to get it to do what I need - this is just the latest rendition. I am new to this forum so hopefully I am formatting correctly.


def zero_to_fifty(max, step, opt_arg = list(range(0,55,2))[::-1]):
  return list(range(0,max,step))


def main():
  result = zero_to_fifty(55,2)
  opt_arg = list(range(0,55,2))[::-1]
  print(result)
  other_result = zero_to_fifty(55,2,opt_arg)
  print(other_result)

  
main()
From this code I get the output:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54]
This output is almost what I need. The second list just needs to be in reverse. Any help would be greatly appreciated. My wording may or may not be confusing so here is the exact problem I am trying to answer:

In assignment 2.4, you created a program that contained two functions, one main() function that called a second zero_to_fifty() function to generate and return a list of numbers. The zero_to_fifty() function took a max parameter for the upper bound of the list to return AND a variable value for the step. To this function, add a third, optional argument that reverses (when called) the list that is returned to main(). Setup your main function so that it (1) calls the zero_to_fifty() function with a max value of 55, a step of 2, and NO REVERSAL, and prints the output and (2) does the same, but this time with reversal turned ON and prints the output. Your output should look like:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54]
[54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0]
Reply
#2
Your third argument (I would call it reverse) should be a boolean (True or False) with a default of False. Then your function should have four steps: create the range, check the reverse/opt_arg parameter, if it is True reverse the list, and then return the list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-31-2018, 02:23 PM)ichabod801 Wrote: Your third argument (I would call it reverse) should be a boolean (True or False) with a default of False. Then your function should have four steps: create the range, check the reverse/opt_arg parameter, if it is True reverse the list, and then return the list.


Thank you sir! That'll do her. Can't believe I spent 10 hours on this. /crying
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb If the argument is not int then make it int! pooyan89 2 1,587 Dec-11-2020, 02:47 PM
Last Post: deanhystad
  creating functions which modify numpy array GSGSGKGK 0 1,564 Dec-15-2019, 07:04 PM
Last Post: GSGSGKGK
  A sign-Reversal Puzzle HY2000 2 2,434 Dec-05-2019, 11:55 AM
Last Post: HY2000
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,407 Mar-05-2018, 03:48 PM
Last Post: mpd

Forum Jump:

User Panel Messages

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