Wikipedia:Reference desk/Archives/Computing/2020 March 2

From Wikipedia, the free encyclopedia
Computing desk
< March 1 << Feb | March | Apr >> March 3 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


March 2[edit]

(Web) development novice[edit]

I'm a CS dropout with a bit of programming experience but not much. It's also been years since I coded anything. I consider myself pretty good at working out the logic side of things but I usually get stuck when trying to figure out the best way to do something or if there is a standard way of achieving a certain result. I want to get back into coding by starting a web development project and already I am unsure how to achieve a desired result.

A basic website usually has for example a logo/home page link, a navigation bar, a footer, and the content of the particular page. So a basic way of doing this would be to include the logo, navigation bar, and footer on each page. However, for a (dynamic) site that is constantly adding content this seems less than ideal.

So I was thinking maybe of having a template php page which includes the logo, nav bar, etc. but then would also have some php code that would include the required content.

I don't want to be given the answer (at least for now) as I'd rather figure it out for myself but am I along the right lines? If not maybe a bit of a nudge in the right direction. What sort of problems would this approach present?

--Polyknot (talk) 05:33, 2 March 2020 (UTC)[reply]

Are you using a PHP-dependent content management framework or system such as Drupal or WordPress? If not, than now is perhaps a good time to switch to Django/Python. The learning curve is not very steep, there are excellent tutorials, the community is very helpful to novices, and in the end you will probably enjoy the greater flexibility.  --Lambiam 08:01, 2 March 2020 (UTC)[reply]
You can use the include function in PHP to include whatever you like. So, you can create a file that makes the navigation bar, such as "nav.inc". Then, include it where you want it: include("nav.inc"); You can have files that include other files. For example, all the stuff that comes before the main contant might be in a file named "top.inc". That file includes the header, styles, css, title bar, nav, etc... Then, everything after the content might be in "bottom.inc". So, your web page becomes include("top.inc"); include("mycontent.inc"); include("bottom.inc");. The content one is where you handle dynamic content. Normally, you query a database, get content, and format it properly. But, you could just as well include a text file. Warning: You will find zealots that claim switching from one programming to another programming language will solve all your problems, including obesity, acne, and erectile dysfunction. The rule is always the same: The language you know is the best language to use. 135.84.167.41 (talk) 12:41, 2 March 2020 (UTC)[reply]
What you want to do, particularly if you're a novice, is use a Web framework. Hand-rolling your own website code becomes increasingly overwhelming as you try to do more complex things. Also, again, if you're a novice, you are virtually guaranteed to fall into all kinds of security pitholes you didn't know about, like SQL injection vulnerabilities. A good framework takes care of all that for you. Once you get handy with it you can start to consider things like writing extensions for the framework. Another piece of advice: good books are often better for learning programming than random stuff off Google. (There are books not just for languages but popular libraries, frameworks, etc.) Once you pick a framework, see what resources are recommended by people experienced with it. Remember that used books and libraries exist if you're cost-conscious. --47.146.63.87 (talk) 02:09, 5 March 2020 (UTC)[reply]

How do I eliminate this standard error?[edit]

Here is a kata/programming practice exercise that I recently did:

https://www.codewars.com/kata/55b3425df71c1201a800009c/train/python

Here is my own solution to this kata:

def findMedian(list):
    list = sorted(list)
    length = len(list)
    if length % 2 == 1:
        halfLen = int(length / 2)
        median = list[halfLen]
    elif length % 2 == 0:
        num1 = int(length / 2) - 1
        num2 = int(length / 2)
        median = int((list[num1] + list[num2]) / 2)
    return median

def findRange(list):
    list = sorted(list)
    range = list[-1] - list[0]
    return range

def findMean(list):
    list = sorted(list)
    sum = 0
    for i in range(0,len(list)):
        sum += list[i]
        i += 1
    mean = sum/len(list)
    return int(mean)

def convertFormat(num):
    num = int(num)
    hours = int(num / 3600)
    minutes = int((num % 3600) / 60)
    seconds = ((num % 3600) % 60)
    return format(hours,'02d')+"|"+format(minutes,'02d')+"|"+format(seconds,'02d')

def stat(strg):
    lisst = strg.split(', ')
    lyst = []
    for item in lisst:
        lyst.append(item.split('|'))
    numSec = []
    for i in range(0,len(lyst)):
        numSec.append([int(lyst[i][0])*3600, int(lyst[i][1])*60, int(lyst[i][2])])
    numSecTotal = []
    for i in range(0,len(numSec)):
        numSecTotal.append(numSec[i][0]+numSec[i][1]+numSec[i][2])
    numSecTotal = sorted(numSecTotal)
    formattedMedian = convertFormat(findMedian(numSecTotal))
    formattedRange = convertFormat(findRange(numSecTotal))+" "
    formattedMean = convertFormat(findMean(numSecTotal))+" "
    str = "Range: "+formattedRange+"Average: "+formattedMean+"Median: "+formattedMedian
    return str

This website actually accepted my solution as valid--which is why this solution of mine (by Futurist110, aka myself) is visible here:

https://www.codewars.com/kata/55b3425df71c1201a800009c/solutions/python/all/newest

However, when I clicked on the "Train Again" button and attempted to once again submit this solution with minor modifications, I got this error message in spite of my program passing all of the tests for it on this website:

 STDERR
Traceback (most recent call last):
  File "main.py", line 75, in <module>
    randomTests()
  File "main.py", line 73, in randomTests
    test.assert_equals(stat(a), stat1221(a))
  File "/home/codewarrior/solution.py", line 41, in stat
    numSec.append([int(lyst[i][0])*3600, int(lyst[i][1])*60, int(lyst[i][2])])
ValueError: invalid literal for int() with base 10: ''

What exactly in this program do I actually need to fix in order to eliminate this error message--and just how exactly do I fix the relevant part of this computer program of mine? Any thoughts on this? Futurist110 (talk) 22:53, 2 March 2020 (UTC)[reply]


  • It looks like you are passing an empty string to the int() function, maybe because of badly formatted input data. Rather than try to debug it for you, I'll suggest a way to debug it yourself: put try/except around the line where the error is happening, and print the relevant values in the event that the exception triggers. So in this case, replace line 41 with:
try:
    numSec.append([int(lyst[i][0])*3600, int(lyst[i][1])*60, int(lyst[i][2])])
except ValueError as e:
    print (e, i, lyst[i])
    raise

Use the printed info to either figure out what is wrong, or figure out what other info you need to diagnose the error, then modify the program again to get the additional info. It will often take quite a few iterations of that to diagnose a bug. Debuggers can avoid some of that, but print-based tracing is something every programmer has to do some of the time.

Unrelated: I notice in your findMean function, you sort the list unnecessarily. I'd have just written sum(list) / len(list). 173.228.123.39 (talk) 01:58, 3 March 2020 (UTC)[reply]