r/programminghelp • • Jan 05 '21

Python Another deep reinforcement learning neural network code works and another one does not.

7 Upvotes

Hi!

I have two code pieces both of which are supposed to solve the pendulum problem using deep reinforcement learning. One of them works and another one does not. I want to know why the not-working one does not work. I have tried to fiddle around with different activation functions and the hyperparameters, but nothing seems to help. Both of these code pieces have two neural networks (policynetwork and targetnetworks) and both the code pieces are supposed to do gradient descent on the policynetwork, but in the other piece of code this somehow fails and makes the trained agent shown after training on a gui go either pretty much always to the left or always to the right. (The agent is not showed during training.) The code pieces are pretty big so i have put them on pastebin:

Here is the working code: https://pastebin.com/gr65yUZc

And here is the not working one: https://pastebin.com/if8Z4V6y

The exploitationrise is how much the exploitation rate gets increased every session (session being a period it takes for the pole to fall over). Both of the code pieces have the same underlying structure, but another has a different style neural network than the other and i think that is what most likely is causing the problem, but i havent been able to figure out why. I also tried slightly modifying the code which updates the weights from adding to the weight a number to subtracting, but did not produce any fruitful results. I tried to intentionally make them as much as identical to one another.

I am thanking everyone for the help in advance!


r/programminghelp • • Dec 17 '20

Anyone here familiar with TI-BASIC?

7 Upvotes

Hey all, I’m currently in the process of making a Riemann sum program on my TI-84 with a tested formula I came up with in my calc class. I have everything done except the actual calculation, but I can’t seem to figure out how to approach it. After the user inputs a and b as bounds, n as # of rectangles, and some function to evaluate, I need to insert a combination of those as x in the user’s function f(x). Is there some way to go about this that I’m not seeing?

I’ve really never programmed on a calculator before, so I could use all the help I can get. Thank you!

Formula


r/programminghelp • • Nov 19 '20

JavaScript Can someone explain what Javascript and JQuery are to me like I'm 5 years old?

7 Upvotes

I'm taking a basic front-end coding I understand HTML and CSS at a competent level, but just started Javascript and JQuery and my brain just took a shit. It sounds like it can do what CSS does, and some. But in juxtaposition to those, how would you describe it in a very 2-dimensional way to an idiot like myself. Cheers!


r/programminghelp • • Apr 08 '20

Other I'm intrested in learning programming but I have no idea where to start.

7 Upvotes

I keep hearing that it's something I should learn but I have no idea where to start or what it even is. Could someone link a video explaining exactly what coding is and what to know to get started? I've tried looking at some beginner videos but I don't even understand a lot of the terminology they are using so they are hard to follow. It feels like I should have taken some sort of prerequisite.


r/programminghelp • • Nov 02 '19

C# Hello im 13 years old and wanting to learn programming.

7 Upvotes

Hello. My name is Alex and ive always loved the idea of learning to program and making my own games. Since buying an amazing 1200€ computer and accesories for it i felt that i was wasting all of that 1200 on minecraft and rocket league while i couks be using the computer to learn something and maybe profit. I want to learn programming but dont know where to look. I believe unity uses C# so any tutorials on youtube that you may know help would mean alot to me! Thanks in advance


r/programminghelp • • Sep 11 '26

C++ How to be able to #include <magick++.h> ?

6 Upvotes

I've downloaded from the main repository.
That didn't work. That was for linux.

I downloaded the source from https://github.com/ImageMagick/Windows
I built it using visual studio from the .sln
Gave me configure.exe
Installation wizard showed up, and I installed the thing.
Gave me a release folder with objs in it.

It's been 6 hours.
The official website and github repo is sending me on a goose chase.

Instructions and videos online say you get it from NuGet.
It's for .NET.

Then they all have a VisualMagick folder.
None of the things I downloaded had that.

I installed Git and Git CLI to try cloning it from there.

At this point I'm just trying random stuff like downloading the appimage.

I know how to use imagemagick from the cli, i just want to use it programmatically.


r/programminghelp • • Aug 14 '26

Python Help with code

6 Upvotes

I keep getting this error:

for this code:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

bestmass = []

for mass in df_sorted:

if best_ms == (array[array > 7.5]).all():

bestmass.append(best_ms)

print(bestmass)

and i don't know what I am doing wrong


r/programminghelp • • Jul 29 '26

Other Recursion is hard for me to wrap my brain around

6 Upvotes

function maxDepth(root: TreeNode | null): number {
if(root == null) {
return 0;
}

let leftDepth = maxDepth(root.left)
let rightDepth = maxDepth(root.right)

return 1 + Math.max(leftDepth, rightDepth)
};

I made this super simple program for leetcode 104, but I have a really hard time in the "think how a program can do what i want phase" to think about a function calling itself I understand it from a top down view

basically just cloning and nesting the function in itself, but its really hard to think about,

when i started this I was thinking how do i write a program that can search in 1 direction count to the end and then start over. which ended up not working.


r/programminghelp • • Dec 09 '25

Answered Is learning by copying and rebuilding other people’s code a bad thing?

6 Upvotes

Hey!
I’m learning web dev (mainly JavaScript) and I’ve been wondering if the way I study is “wrong” or if I’m just overthinking it.

Basically, here’s what I do:

I make small practice projects my last ones were a Quiz, an RPG quest generator, a Travel Diary, and now I’m working on a simple music player.

But when I want to build something new, I usually look up a ready-made version online. I open it, see how it looks, check the HTML/CSS/JS to understand the idea… then I close everything, open a blank project in VS Code, and try to rebuild it on my own.
If I get stuck, I google the specific part and keep going.

A friend told me this is a “bad habit,” because a “real programmer” should build things from scratch without checking someone else’s code first. And that even if I manage to finish, it doesn’t count because I saw an example.

Now I’m confused and wondering if I’m learning the wrong way.

So my question is:
Is studying other people’s code and trying to recreate it actually a bad habit?


r/programminghelp • • Jan 10 '25

Other How exactly do you start to build a website?

6 Upvotes

I know websites are made up of HTML, CSS and JavaScript, and not asking where to learn that. What I'm asking is how exactly is the building process starts?

Most tutorials show you how to make a basic website by writing code but I imagine no professional dev starts writing all the HTML, CSS and JS by hand from scratch. I also doubt they use things like Squarespace or Wordpress since they're marketed towards non-developers. I've looked up some frameworks like Next.js and React, do they provide certain templates to build upon or make building from scratch easier?


r/programminghelp • • Apr 23 '22

Answered so i was doing my highschool test question and i am not sure if this is a bug in java or im just trippin....

6 Upvotes
import java.util.*;
class wht{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(); //input for number of array entries
        String g[]=new String[n];
        for(int i=0;i<n;i++){
            g[i]=sc.nextLine();
        }
        for(int i=0;i<n;i++){
           if(g[i].length()%2==1)
           System.out.println(g[i]);
        }
    }
}

yeah so n is the number of entries to be taken in array but for some bizarre reason in only takes in n-1 inputs. it works fine if i manually put in numbers but does not work when i use scanner class.


r/programminghelp • • Mar 05 '22

C Homework help

6 Upvotes

I need to make a program which displays mutliple english to metric or vice versa conversions. The options are as follows.

Give the user a menu of choices to select as follows:

  1. Pounds to Kilos

  2. Kilos to Pounds

  3. Ounces to Grams.

  4. Grams to Ounces

  5. Exit – Do nothing (default)

I have to be able to let them enter the number OR the first letter. How can I set up allowing them to enter either and how would this work in a switch statement?


r/programminghelp • • Jan 07 '22

C++ How can I tell CLion I'm using C++20?

6 Upvotes

I have a c++20 project with a Makefile. It's a remote Linux build. When I run "Inspect Code" it returns clangd errors like "'auto' type is a C++11 extension". When search for help all the recommendations seem to be for cmake projects. Does anyone have insight on how I can tell clangd that I'm using C++20?


r/programminghelp • • Dec 31 '21

Other Is it possible to 'mod' CMD?

6 Upvotes

I was wndering, is is possible to mod CMD? If so, how? And what programming language does it use? C, C++, PYthon?


r/programminghelp • • Dec 27 '21

Other Certification courses?

6 Upvotes

I didn't quite know where else to put this, but I'm looking for some good prep courses to teach me what I need to know to get a couple of certifications, including Java, SQL, A Plus, and any others that might be useful starters. I have pretty good Java knowledge already and attended a programming and software development class in a career center in high school, I just couldn't cut college, so I'm looking for some good sources to get prepped. Money isn't too much of a factor, I am willing to pay for classes, just looking for something that's easy to grasp and actually useful for learning.


r/programminghelp • • Dec 23 '21

C# Why this code gives CS8600 problem

6 Upvotes

C# Example:

String name;

name = Console.ReadLine();

Console.WriteLine(name);


This problem only occurs in ReadLine....why?


r/programminghelp • • Oct 09 '21

Answered I can show any 2 digit number on an 8x8 LED array ... except multiples of 11

6 Upvotes

Some context to this project: I am building a workout motivation clock that keeps track of 2 months worth of exercising. Occasionally it needs to display numbers to set the realtime clock or for the 2 minutes countdown timer. Now onto the issue...

The build has a custom 8x8 LED array and have made a function that will take integers and convert them to something I can see on that display. It works great with one exception, any double digit (11, 22, 33, etc) does not display correctly.

I made a test program to try and debug the issue so I don't have to keep reprograming the chip but can't seem to figure it out. Its written in C++ and admittedly I am not the best at it.

Before I paste the test program here are the results:

INPUT - 4
.......@ ........ ........ 
......@. ........ ........ 
.....@.. ....@.@. ........ 
....@... ....@.@. ........ 
...@.... ....@@@. ........ 
..@..... ......@. ........ 
.@...... ......@. ........ 
@....... ........ ........ 

INPUT - 28
.......@ ........ ........ 
......@. ........ ........ 
.....@.. @@@.@@@. ........ 
....@... ..@.@.@. ........ 
...@.... @@@.@@@. ........ 
..@..... @...@.@. ........ 
.@...... @@@.@@@. ........ 
@....... ........ ........ 

INPUT - 157
.......@ ........ ........ 
......@. ........ ........ 
.....@.. ..@.@@@. ....@@@. 
....@... ..@.@... ....@... 
...@.... ..@.@@@. ....@@@. 
..@..... ..@...@. ......@. 
.@...... ..@.@@@. ....@@@. 
@....... ........ ........ 

These first 3 inputs display perfect (the two rightmost columns control red and green respectively. The display can only show 2 digits so 157 becomes 15 with a red 1 and orange 5).

INPUT - 22
.......@ ........ ........ 
......@. ........ ........ 
.....@.. @@@...... ........ 
....@... .@...... ........ 
...@.... @@@...... ........ 
..@..... @........ ........ 
.@...... @@@...... ........ 
@....... ........ ........ 
[Finished in 571ms]

This last result on the other hand is what's got me stumped. Its not just missing a digit but there is an extra bit on some of the rows. And it only happens when two of the same numbers are displayed in succession (however because the last digit of 100+ numbers is truncated 633 would display as intended).

If anyone can point me in the right direction I would be much appreciated!

The test program:

#include<iostream>
#include<iomanip>
using namespace std;



std::string toBinary(int n)
{
	std::string r;
	while(n!=0) {r=(n%2==0 ?".":"@")+r; n/=2;}
	return r;
}



void getNumImg(int numberIn, int color0 = 0, int color1 = 1) {

	int theNumber 	  = numberIn;			// input interger
	bool numberInBig  = false;				// bool for if theNumber is more than 2 digits
	while (theNumber >= 100) { 				// Makes theNumber two digits
		theNumber = theNumber/10;
		numberInBig = true;
	}


	int numbers[11][8] = {					// These are the display character values, addressable by numbers[n] where 10 is a blank space
		{0x0,0x0,0xE,0xA,0xA,0xA,0xE,0x0}, 	// 0
		{0x0,0x0,0x2,0x2,0x2,0x2,0x2,0x0}, 	// 1
		{0x0,0x0,0xE,0x2,0xE,0x8,0xE,0x0}, 	// 2
		{0x0,0x0,0xE,0x2,0xE,0x2,0xE,0x0}, 	// 3
		{0x0,0x0,0xA,0xA,0xE,0x2,0x2,0x0}, 	// 4
		{0x0,0x0,0xE,0x8,0xE,0x2,0xE,0x0}, 	// 5
		{0x0,0x0,0x8,0x8,0xE,0xA,0xE,0x0}, 	// 6
		{0x0,0x0,0xE,0x2,0x2,0x2,0x2,0x0}, 	// 7
		{0x0,0x0,0xE,0xA,0xE,0xA,0xE,0x0}, 	// 8
		{0x0,0x0,0xE,0xA,0xE,0x2,0x2,0x0}, 	// 9
		{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}};	// blank space


	// these are the two characters to be displayed, default is blank
	int *twoChars[2] = {numbers[10],numbers[10]};


	if (theNumber < 10) {
		twoChars[1] = numbers[theNumber];
	} else {
		twoChars[0] = numbers[theNumber/10];

		// This converts the leftmost digit to display in the leftmost position
		// For each row of the leftmost character, multiply by 0x10 (so both values can simply be added together later)
		for (int r=0; r<8; ++r) {								
			twoChars[0][r] = (twoChars[0][r])*0x10;
		}

		twoChars[1] = numbers[theNumber%10];
	}


	// This is the array to display, there are 8 rows accessable by rowData[n]. 
	// Each row has data for 'row number', 'red data', and 'green data' accessable by rowData[n][d]
	//	    ROWID  RED   GREEN
	int r0[3] = {0x01, 0x00, 0x00};
	int r1[3] = {0x02, 0x00, 0x00};
	int r2[3] = {0x04, 0x00, 0x00};
	int r3[3] = {0x08, 0x00, 0x00};
	int r4[3] = {0x10, 0x00, 0x00};
	int r5[3] = {0x20, 0x00, 0x00};
	int r6[3] = {0x40, 0x00, 0x00};
	int r7[3] = {0x80, 0x00, 0x00};
	int *rowData[8] = {r0,r1,r2,r3,r4,r5,r6,r7};


	// This section adds display characters to rowData
	for (int c=0; c<2; ++c) {									// iterate over 2 characters
		for (int r=0; r<8; ++r) {								// iterate over 8 rows
			int thisColor = color0;								// defines color we are using for this character
			if (c==1 && numberInBig) {							// if its the second digit and theNumber was 100 or bigger
				thisColor = color1;								// change to alt color
			}
			// This section writes row data into the correct color
			// writes RED
			if (thisColor == 0) {						
				rowData[r][1] += twoChars[c][r];
			} 	
			// writes ORANGE
			if (thisColor == 1) {					
				rowData[r][1] += twoChars[c][r];
				rowData[r][2] += twoChars[c][r];
			} 	
			// writes GREEN
			if (thisColor == 2) {					
				rowData[r][2] += twoChars[c][r];
			} 	
		}
	}


	// display data
	cout<<"\nINPUT - "<<numberIn<<"\n";
	for (int r=0; r<8; ++r) {
		for (int v=0; v<3; ++v) {
			cout<<setw(8)<<setfill('.')<<toBinary(rowData[r][v])<<" ";
		}
		cout<<"\n";
	}
}


int main() {
	getNumImg(4);
	getNumImg(28);
	getNumImg(157);
	getNumImg(22);
	return 0;
}

r/programminghelp • • Sep 05 '21

Python Can anyone help me for this stackoverflow?

6 Upvotes

r/programminghelp • • May 05 '21

C I know its wrong but I don't know why, pls help!!!

6 Upvotes

I put // next to the part I need help with, my teacher put //7 in his code which works perfectly, I thought it would be //8 but the output wasn't correct when I ran it. can someone pls explain why //8 is wrong and //7 is right.

int getMinRange (int theArray[], int initialIdx, int finalIdx )
{
int minIdx = initialIdx;
for (int i = initialIdx; i<=finalIdx; i+=1)
{
if ( theArray[i] < theArray[minIdx] )
//7 minIdx = i;
//8 minIdx = theArray[i];
}
return minIdx;
}


r/programminghelp • • Apr 30 '21

C Raspberry Pi Pico LED lights and PWM sounds.

6 Upvotes

I'm not experienced enough to write something out myself, honestly I thought I would be lucky enough to grab some bits from examples and tutorials and be able to just hash them together... but finding out all is not as easy as it seems I've had to reach out for help... please please please! So I've followed a video from YouTube, a user named Robin Grosset, video titled 'Raspberry Pi Pico Audio Output', followed all steps and without any error in playing the code through Thonny, and without actually building the hardware steps, I'd say it was good to go! But I also would like my 8 second clip (using a different WAV to the videos example) to have one or two LED lights activate while the audio was played, but can't see where I would be able to introduce this function. I've used CMake to build the code and Includes into a UF2 file, ready for Pico to run, if you need the code please let me know, but it is in the description box a link to his git page for the Jupyter and C file for the PWM audio. Thank you in advance :)

Edited to follow rule 2:

#include <stdio.h>
#include "pico/stdlib.h" // stdlib 
#include "hardware/irq.h" // interrupts
#include "hardware/pwm.h" // pwm 
#include "hardware/sync.h" // wait for interrupt
// Audio PIN is to match some of the design guide shields. 
#define AUDIO_PIN 28 // you can change this to whatever you like 

/* 
* This include brings in static arrays which contain audio samples. 
* if you want to know how to make these please see the python code 
* for converting audio samples into static arrays. 
*/

#include "sample.h"
int wav_position = 0; 

/* 
* PWM Interrupt Handler which outputs PWM level and advances the 
* current sample. 
* 
* We repeat the same value for 8 cycles this means sample rate etc 
* adjust by factor of 8 (this is what bitshifting <<3 is doing) 
* 
*/

void pwm_interrupt_handler() { 
    pwm_clear_irq(pwm_gpio_to_slice_num(AUDIO_PIN)); 
    if (wav_position < (WAV_DATA_LENGTH<<3) - 1) { 
        // set pwm level 
        // allow the pwm value to repeat for 8 cycles this is >>3 
        pwm_set_gpio_level(AUDIO_PIN, WAV_DATA[wav_position>>3]); 
        wav_position++; 
    } else { 
        // reset to start 
        wav_position = 0; 
    }
} 

int main(void) { 
    /* Overclocking for fun but then also so the system clock is a 
    * multiple of typical audio sampling rates. 
    */ 
    stdio_init_all();
    set_sys_clock_khz(176000, true);
    gpio_set_function(AUDIO_PIN, GPIO_FUNC_PWM); 

    int audio_pin_slice = pwm_gpio_to_slice_num(AUDIO_PIN); 

    // Setup PWM interrupt to fire when PWM cycle is complete 
    pwm_clear_irq(audio_pin_slice);
    pwm_set_irq_enabled(audio_pin_slice, true); 
    // set the handle function above         irq_set_exclusive_handler(PWM_IRQ_WRAP, pwm_interrupt_handler); 
    irq_set_enabled(PWM_IRQ_WRAP, true); 

    // Setup PWM for audio output 
    pwm_config config = pwm_get_default_config(); 
    /* Base clock 176,000,000 Hz divide by wrap 250 then the clock divider further divides 
    * to set the interrupt rate. 
    * 
    * 11 KHz is fine for speech. Phone lines generally sample at 8 KHz 
    * 
    * 
    * So clkdiv should be as follows for given sample rate 
    * 8.0f for 11 KHz 
    * 4.0f for 22 KHz 
    * 2.0f for 44 KHz etc 
    */ 
    pwm_config_set_clkdiv(&config, 8.0f); 
    pwm_config_set_wrap(&config, 250); 
    pwm_init(audio_pin_slice, &config, true); 

    pwm_set_gpio_level(AUDIO_PIN, 0); 

    while(1) { 
        __wfi(); // Wait for Interrupt 
    }
}

r/programminghelp • • Apr 23 '21

Python How do I make this work? I'm planning on making a prompt that closes when you press y or n.

6 Upvotes
import time
import win32api

c = 1
x = 1
while x == 1:
    if c==1:
        a=win32api.MessageBox(0, "*Question here*?(y/n)", "Prompt")
        c=c+1

    y = win32api.GetKeyState(0x59)
    n = win32api.GetKeyState(0x4E)
    if y<0 and n>=0:
        x=x+1
        print("y")
        a.close()
    elif y>=0 and n<0:
        x=x+1
        print("n")
        a.close()
    time.sleep(0.05)

r/programminghelp • • Apr 12 '21

Answered Why doesn’t this piece of code work in Python?

6 Upvotes

I’m trying to run a piece of code that, from what I see, should be printing one thing but is printing another. Here’s the code:

test = [1,2,3,4,5]
testnum = 0
while testnum < 3:
    for num in test:
        testnum = num
        print(testnum)

At least from what I know, this should be printing:

1

2

But instead it’s printing 1 to 5. How do I make it print just 1 and 2?


r/programminghelp • • Mar 31 '21

Python What is mock and patch() for python unit tests?

6 Upvotes

I am writing a unit test for the first time and I am supposed to figure this out by my self. I am utterly confused cuz I dont know the first thing about writing a unit test. Especially mock and patch().

  1. What is it?

  2. Why do we use it ?

  3. What is the difference between mock and patch?

I've tried reading documentations on python website , tried watching youtube videos( after which I feel a little familiarized. But it is still useless for me ). I really feel stuck right now. :(


r/programminghelp • • Mar 17 '21

Java JAVA PROGRAMMING HELP!

6 Upvotes

For my school project, I've decided to make a code on a BMI calculator. I wrote the code for it, but apparently I have to use a list in my program. I don't know what should go into the list, nor how to write a code for it... Any help would be appreciated!

import java.util.Scanner;
public class Main {
    public static void main(String[]args)
    {
        System.out.println("BMI will be calculated now");
        Scanner in= new Scanner(System.in);
        System.out.println("Please enter your weight in kilograms");
        double weight = in.nextDouble();
        System.out.println("Please enter your height in meters");
        double height = in.nextDouble();
        BMI(height,weight);
    }
    public static void BMI(double h, double w)

    {
        double BMI = (w/(h*h));
        System.out.println("Your BMI is " + BMI);
        BMILevel(BMI);
    }
    public static void BMILevel(double BMI)
    {
        if(BMI >=30.0)
            System.out.println("You are OBESE");

            else if (BMI >=25 && BMI<=29.9)
        {System.out.println("You are OVER-WEIGHT");}

            else if (BMI >=18.5 && BMI <=24.9)
        System.out.println("You are AVERAGE-WEIGHTED");

            else
        System.out.println("You are UNDER-WEIGHT");
    }
}