Python Forum
trying an online challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying an online challenge
#1
I recently started learning Python. I have watched several YouTube vids, read some documentation, and experimented on my own. I'm currently trying to complete the first challenge for a website(not able to find in TOS if posting links or URL's is approved). The paraphrased  challenge is:

Have a function take the num parameter and return the factorial. The range is between 1 and 18, and input is always an integer.

Here's the first sequence I tried:
def factorial():
   num = int(input("Pick a number between 1 and 18 "))
   n = num - 1
   while n>0:
      num =  num*n
      n = n-1
   return num

print(factorial())
When I submitted, it hung for a few minutes, said I was wrong, and the results indicated it was supposed to test all values from 1 to 10. I figured out there was no actual input because I told it to run code, and it's still running 20 minutes later. I assume it's waiting for input to define num. I reread the challenge, saw "range," and thought "Oh, I need a range type." So then I try this:
def FirstFactorial(num): 
   n = num - 1
   while n>0:
       num = num*n
       n = n-1
   return num
   
for x in range (1,20,1):
   while x != 19:
       num =  x
       print(FirstFactorial(num))
       x = x+1
All I get back is an endless list of "1." Obviously, I'm missing something with the way ranges work, so I just simplify the loop and try this:

def FirstFactorial(num): 
   n = num - 1
   while n>0:
       num = num*n
       n = n-1
   return num

x = 1    
while x != 19:
   num =  x
   print(FirstFactorial(num))
   x = x+1    
I get the expected output, submit it, and it still says all outputs are wrong. The site allows looking at other submissions, but requires a premium account to do so. I thought I should put this in coding help, but it seemed to fit here better with the restrictions on the challenge. Any advice on what I might be missing? I've read through range type on docs.python.org a few times and I'm not seeing anything for how to actually use the range for managing loops.
Reply
#2
I tink they are just asking for a function (maybe with a specif name) that takes one number and return its factorial. The input range they mention is only to make it simple for your (18! fits in a long integer, no need for high accuracy integers). So I would expect the code to be just
def factorial(n):
    result=# insert your code here
    return result;
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
The "input" range they mention is actually the test parameters. It doesn't just test one value, it tests 1 through 10. The site calls them inputs, but as you can see from my code and the results of the page running the code, it never actually gets past the point where I ask for input, so there are no inputs.

Is your example based on the incorrect conclusion that I only need one output, "#insert your code here" supposed to cover multiple lines, or is there a way to get multiple outputs with only 1 value?
Reply
#4
You were on the right track with your first example, almost. Then you were correct in thinking you needed to use 'range'. Then you sort of went berserk. Many times in reply's you will see the advise "add print statements".  This helps you to "see" what the script "sees". Once your satisfied, you can delete the print statements.

I've taken the liberty of modifying your code a bit:

def factorial(number):
   for n in range(1, number):    # Gives range 1 thru number - 1
       print('number {} times {}'.format(number, n))    # Go ahead and delete this line
       number *= n
       print("number = ", number)    # Go ahead and delete this line
   return number

selection = int(input("Pick a number between 1 and 18: "))
result = factorial(selection)
print("{} objects can be arranged {} different ways".format(selection, result))
Output:
Pick a number between 1 and 18: 5 number 5 times 1 number =  5 number 5 times 2 number =  10 number 10 times 3 number =  30 number 30 times 4 number =  120 5 objects can be arranged 120 different ways
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
So, I'm really close but haven't grasped a concept for this yet. I'll keep reading and learning and try it out again later.
Reply
#6
I finally figured out how the website works and the code that passed the challenge is:
def FirstFactorial(num): 
   n = num - 1
   while n>0:
       num = num+n
       n = n-1
   return num

print (FirstFactorial(raw_input()))
Why am I posting the answer? Because this challenge is about 10% your ability to write some code, and 90% figuring out test parameters that are not provided. This is important to note because it's a good life metaphor. You can be really good at something, but you have to be able to adapt your skills to the situation for it to matter. And NO ONE is going to tell you what you need to know to adapt your skill set to the situation, and sometimes they will lie to you. You need to be able to pick up the clues or you will fail. So, by looking at what the challenge says it wants and what the correct setup is, we can figure what the actual parameters of the challenge are:
First, WTF is raw_input()? I'm sure someone who knows more about python than I do currently can explain it in general(I haven't checked the docs yet); specifically, it is the test values the page will be injecting into your code. If raw_input() is replaced by anything, the code will fail because the site is no longer able to inject what is wants, even though when you run the code you get the expected output.
Secondly, the range they mention isn't a range. The site runs the code once for each raw_input() it injects. In this case, it runs the code 10 times, once for each value between 1 and 11. If it gets the expected output, you pass the challenge for that run. Each successful run is worth .5 points. This means if you use a range, it will fail every time because you are running the code for the range of all values instead of once for each value.

Actual test parameters: Get an output that is a factorial of a value injected into the code to replace raw_input()

Once I configured my code around the actual test parameters instead of the reported parameters, it passed the challenge. Now that I know it's only testing if raw_input() is included where the site wants it to be, and it gets the expected output each time it runs, I've been able to pass a couple of other challenges without problem.

Other things I learned:  1) Don't use "while" when you already running "for" in a range. It's redundant and can cause problems. 2) Same thing with redefining a variable that was defined by the range. It will break your range. 3) Don't make things harder than you have to.
num = raw_input()
n=num-1
while n>0:
   num=num*n
   n=n-1

print(num)
works better, because you are using fewer lines of code, and removing a few possibilities for mistakes. The second one passed the challenge too, BTW.
Reply
#7
In Python v2, input() reads a string and tries to interpret it. For instance an answer of 2+3 to input() will make input() return the int object 3. By contrast, raw_input() returns the entered string, no more, no less, so it would return the str object 2+3.

This behavior of input() is now considered dangerous and not very usable anyway so In Python v3, input() behaves likes v2 raw_input().
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#8
So that tells me what version of python the site uses. I was getting syntax errors for end='' I use python 3.6 and when testing my code it worked great, and I was so confused why they had a syntax error.
Reply
#9
(Jun-12-2017, 11:38 AM)tozqo Wrote: I'm currently trying to complete the first challenge for a website(not able to find in TOS if posting links or URL's is approved).

As long as the links are on-topic, and in this case a python challenge seems perfectly fine Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Team Chooser Challenge kanchiongspider 3 2,347 Jun-02-2020, 04:02 AM
Last Post: kanchiongspider
  Meal cost challenge Emekadavid 3 2,830 Jun-01-2020, 02:01 PM
Last Post: Emekadavid
  Appending To Files Challenge erfanakbari1 3 2,916 Mar-27-2019, 07:55 AM
Last Post: perfringo
  Problem with a basic if challenge erfanakbari1 2 1,975 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1

Forum Jump:

User Panel Messages

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