Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Compiles Python code with no error but giving out no output - what's wrong with it?

Am running Python 3.8.10 in a 32-bit Windows 7 computer with only 2GB memory and no video card (not even an NVIDIA) - it's a two decade old netbook...
Tried running the following code:

Code:
import numpy as np
from scipy import ndimage

def predict_next_string(database):
  # Split the database into a list of strings
  strings = np.array(database.split('\n'))

  #last_two_strings = ndimage.laplace(strings.astype(bool), mode='constant')

  # Check if there are at least two strings
  if len(last_two_strings) < 2:
      print ("Not enough strings in the database,")
      return

  # Get the second-to-the-last string in the list
  last_string = last_two_strings[-1]

  # Split the last string into a list of integers
  last_string_numbers = [int(x) for x in last_string.split()]

  # Increment each number by one and return the resulting string
  predicted_string = ' '.join([str(x + 1) for x in last_string_numbers])

  print("The predicted next string is", predicted_string)
 
database = """5 3 4 5 1 2
1 3 1 5 0 4
2 3 0 5 1 2
0 5 4 0 5 2
1 1 0 5 2 4
4 1 4 3 0 0
4 3 3 3 2 3
2 3 3 1 4 1
4 2 3 3 1 1
2 3 1 4 4 2
0 2 3 4 2 2
3 4 5 4 5 1
2 4 3 1 0 2
2 1 2 2 0 5
4 4 1 0 1 3
2 2 5 4 0 2
3 2 2 2 4 3
4 1 3 3 3 2
5 3 3 1 3 5
1 0 2 2 5 3
1 3 3 5 0 2
2 3 4 1 1 0
0 0 4 2 4 1
4 3 2 4 1 3
3 4 4 1 1 4
1 2 4 1 5 4
5 5 4 2 0 5
5 4 1 4 5 5
4 4 4 2 2 0
1 3 1 2 0 1
1 2 4 4 5 5
3 2 1 4 5 5
5 1 5 2 5 4
1 2 4 1 5 2
5 5 4 2 0 5
5 4 1 4 5 5
4 4 4 2 2 0
1 3 1 2 0 1
1 2 4 4 5 5
3 2 1 4 5 5
5 1 5 2 5 4
0 1 5 5 5 4
3 3 1 5 3 5
"""

It is meant to predict the next string of 6 numbers from 0 to 5.

There are no errors when I compiled it - but for some reason, it's not giving out an output.
What could be wrong with the code?
 

Attachments

  • source code output_03202023.jpg
    source code output_03202023.jpg
    70.5 KB · Views: 0
As stated in python.io forum, you need to call the function.
Basic example

Python:
# Basic function
def my_function():
    print('function has been called')

# Call the function
my_function()
 
So, in revising the code, must I do it this way?

Python:
import numpy as np
from scipy import ndimage

def predict_next_string(database):
  # Split the database into a list of strings
  strings = np.array(database.split('\n'))

  #last_two_strings = ndimage.laplace(strings.astype(bool), mode='constant')

  # Check if there are at least two strings
  if len(last_two_strings) < 2:
      print ("Not enough strings in the database,")
      return

  # Get the second-to-the-last string in the list
  last_string = last_two_strings[-1]

  # Split the last string into a list of integers
  last_string_numbers = [int(x) for x in last_string.split()]

  # Increment each number by one and return the resulting string
  predicted_string = ' '.join([str(x + 1) for x in last_string_numbers])

  print("The predicted next string is", predicted_string)

  predict_next_string(database)
 
database = """5 3 4 5 1 2
1 3 1 5 0 4
2 3 0 5 1 2
...

0 1 5 5 5 4
3 3 1 5 3 5
"""

...or this way?

Python:
import numpy as np
from scipy import ndimage

def predict_next_string(database):
  # Split the database into a list of strings
  strings = np.array(database.split('\n'))

  #last_two_strings = ndimage.laplace(strings.astype(bool), mode='constant')

  # Check if there are at least two strings
  if len(last_two_strings) < 2:
      print ("Not enough strings in the database,")
      return

  # Get the second-to-the-last string in the list
  last_string = last_two_strings[-1]

  # Split the last string into a list of integers
  last_string_numbers = [int(x) for x in last_string.split()]

  # Increment each number by one and return the resulting string
  predicted_string = ' '.join([str(x + 1) for x in last_string_numbers])

  print("The predicted next string is", predicted_string)
 
database = """5 3 4 5 1 2
1 3 1 5 0 4
2 3 0 5 1 2
...

0 1 5 5 5 4
3 3 1 5 3 5
"""

  predict_next_string(database)

Pardon, I just don't have an idea which is which....
 
There are still errors but here your code rearranged a little

Python:
import numpy as np
from scipy import ndimage

database = """5 3 4 5 1 2
1 3 1 5 0 4
2 3 0 5 1 2
0 5 4 0 5 2
1 1 0 5 2 4
4 1 4 3 0 0
4 3 3 3 2 3
2 3 3 1 4 1
4 2 3 3 1 1
2 3 1 4 4 2
0 2 3 4 2 2
3 4 5 4 5 1
2 4 3 1 0 2
2 1 2 2 0 5
4 4 1 0 1 3
2 2 5 4 0 2
3 2 2 2 4 3
4 1 3 3 3 2
5 3 3 1 3 5
1 0 2 2 5 3
1 3 3 5 0 2
2 3 4 1 1 0
0 0 4 2 4 1
4 3 2 4 1 3
3 4 4 1 1 4
1 2 4 1 5 4
5 5 4 2 0 5
5 4 1 4 5 5
4 4 4 2 2 0
1 3 1 2 0 1
1 2 4 4 5 5
3 2 1 4 5 5
5 1 5 2 5 4
1 2 4 1 5 2
5 5 4 2 0 5
5 4 1 4 5 5
4 4 4 2 2 0
1 3 1 2 0 1
1 2 4 4 5 5
3 2 1 4 5 5
5 1 5 2 5 4
0 1 5 5 5 4
3 3 1 5 3 5
"""

def predict_next_string(database):
  # Split the database into a list of strings
  strings = np.array(database.split('\n'))

  last_two_strings = ndimage.laplace(strings.astype(bool), mode='constant')


  # Check if there are at least two strings
  if len(last_two_strings) < 2:
      print ("Not enough strings in the database,")
      return

  # Get the second-to-the-last string in the list
  last_string = last_two_strings[-1]

  # Split the last string into a list of integers
  last_string_numbers = [int(x) for x in last_string.split()]

  # Increment each number by one and return the resulting string
  predicted_string = ' '.join([str(x + 1) for x in last_string_numbers])

  print("The predicted next string is", predicted_string)
 
predict_next_string(database)
 
I hope you won't mind me asking these additional side questions:

1) The code here somewhat dabbles into machine learning, but in spite of my hardware limits (pretty old CPU and 2GB memory in a two-decade old Samsung netbook) and software limits (Python 3.8.10 running in a 32-bit Windows 7 OS), is there a way to make a semi-machine learning code like this bring out expected results? What machine learning PYthon libraries do I need to install aside from versions of numpy and pandas that are compatible with Python 3.8.10?

2) I can't buy a new computer right now with specs geared for machine learning but if I'm told to use an online service that can run Python source codes dealing with machine learning, what do you recommend that can work well with the current computer I have now?
Some online services require I have a 64-bit Windows 8 OS or newer (so I can run updated web browsers in it), while the others require I need a credit card to sign up. If it's free, it's got a hell-load of limits that's discouraging to use... (weeping emoticon)
 
Maybe someone more experienced in the field can help. I have no knowledge of machine learning. Sorry I am not able to answer your questions.
How about question number 2?

Anyway, something came up: just ran the code you gave me and it yielded this error message:
Code:
File "script.py", line 5
    strings = database.split ("\n")
    ^
IndentationError: expected an indented block

What does that mean?
 
How about question number two?
Anyway, just complied the code you gave me and this error message showed up:

Code:
File "script.py", line 5
    strings = database.split ("\n")
    ^
IndentationError: expected an indented block

What does that mean?
 
Well, you must have a syntax error somewhere ;)

What I do when I want to try a machine learning project is I spin up an AWS p-type instance using the Amazon Linux AMI with Tesla NVIDIA drivers installed. I think it runs me something like $3 an hour to run or so.
 
Well, you must have a syntax error somewhere ;)

What I do when I want to try a machine learning project is I spin up an AWS p-type instance using the Amazon Linux AMI with Tesla NVIDIA drivers installed. I think it runs me something like $3 an hour to run or so.

Regarding that AWS service, can't afford that.
$3 per hour converted into our currency is damn expensive...
 
Maybe someone more experienced in the field can help. I have no knowledge of machine learning. Sorry I am not able to answer your questions.

By the way, I made some edits to the code after reading your ideas here:

Python:
import numpy as np
from scipy import ndimage


database = """5 3 4 5 1 2
1 3 1 5 0 4
2 3 0 5 1 2
0 5 4 0 5 2
1 1 0 5 2 4
4 1 4 3 0 0
4 3 3 3 2 3
2 3 3 1 4 1
4 2 3 3 1 1
2 3 1 4 4 2
0 2 3 4 2 2
3 4 5 4 5 1
2 4 3 1 0 2
2 1 2 2 0 5
4 4 1 0 1 3
2 2 5 4 0 2
3 2 2 2 4 3
4 1 3 3 3 2
5 3 3 1 3 5
1 0 2 2 5 3
1 3 3 5 0 2
2 3 4 1 1 0
0 0 4 2 4 1
4 3 2 4 1 3
3 4 4 1 1 4
1 2 4 1 5 4
5 5 4 2 0 5
5 4 1 4 5 5
4 4 4 2 2 0
1 3 1 2 0 1
1 2 4 4 5 5
3 2 1 4 5 5
5 1 5 2 5 4
0 1 5 5 5 4
3 3 1 5 3 5
5 3 3 4 3 5
"""
def predict_next_string(database):
    # Split the database into a list of strings
    strings = database.strip().split('\n')


    # Check if there are at least two strings
    if len(strings) < 2:
        print("Not enough strings in the database.")
        return


    # Convert the strings to a 2D array of integers
    arr = np.array([list(map(int, s.split())) for s in strings])


    # Apply Laplace filter to the last two rows
    last_two_rows = arr[-2:]
    last_two_rows_laplace = ndimage.laplace(last_two_rows.astype(bool), mode='constant')


    # Increment each number by one and return the resulting string
    predicted_string = ' '.join([str(x + 1) for x in last_two_rows_laplace.ravel()])


    print("The predicted next string is:", predicted_string)


predict_next_string(database)


I used an online code editor (GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++) but I'm not getting even an output from there....

Any idea as to why there's still no output?
 
What is the expected output? This is what I get.
Output:
The predicted next string is: 2 2 2 2 2 2 2 2 2 2 2 2
That's funny - when I use the online editor I mentioned here, I don't get that output.
And why is it on your end you're getting more than six characters? It should display only "2 2 2 2 2 2" - worse still, why is it only purely "2"? It should show like "5 4 4 2 3 0", "0 0 0 3 3 1", or something like that (by the way, those outputs are just theoretical examples - haven't actually got that kind of output yet...)
 
On my windows box right now
Device name DESKTOP-E0UK08H
Processor Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz 3.70 GHz
Installed RAM 32.0 GB
System type 64-bit operating system, x64-based processor
Display Resolution 2560x1440
Graphics Radeon RX 580 Series
 
On my windows box right now
Device name DESKTOP-E0UK08H
Processor Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz 3.70 GHz
Installed RAM 32.0 GB
System type 64-bit operating system, x64-based processor
Display Resolution 2560x1440
Graphics Radeon RX 580 Series
Seriously, your unit is enough to make me cry an ocean! 😭
I'd kill just to have that kind of computer!

What Python version are you using? 🫤

Wait, are you even using Python - or, are you working using the latest version of Anaconda? 🤔
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom