PLEASE HELP: creating a generator (astro signs)

hello!
so im new to processing and i have no clue what im doing and im supposed to be creating a generator of some sort. ive used else, if statements and i think that works. so when i put in my values i get the answer printed out into the console but how do i correlate that to display my answer onto the window? (sorry if the question doesn’t make sense)

username = ‘anon’
day = 20
month = ‘january’
x = ‘astro sign’

if month == ‘december’:
x = ‘Sagittarius’ if (day < 22) else ‘capricorn’
elif month == ‘january’:
x = ‘Capricorn’ if (day < 20) else ‘aquarius’
elif month == ‘february’:
x = ‘Aquarius’ if (day < 19) else ‘pisces’
elif month == ‘march’:
x = ‘Pisces’ if (day < 21) else ‘aries’
elif month == ‘april’:
x = ‘Aries’ if (day < 20) else ‘taurus’
elif month == ‘may’:
x = ‘Taurus’ if (day < 21) else ‘gemini’
elif month == ‘june’:
x = ‘Gemini’ if (day < 21) else ‘cancer’
elif month == ‘july’:
x = ‘Cancer’ if (day < 23) else ‘leo’
elif month == ‘august’:
x = ‘Leo’ if (day < 23) else ‘virgo’
elif month == ‘september’:
x = ‘Virgo’ if (day < 23) else ‘libra’
elif month == ‘october’:
x = ‘Libra’ if (day < 23) else ‘scorpio’
elif month == ‘november’:
x = ‘scorpio’ if (day < 22) else ‘sagittarius’

print(x)

You can use text( x , 100, 100);

Does this work??

Try

if (day < 22) x = ‘Sagittarius’ else x= ‘capricorn’

Hi,

Welcome to the forum! :wink:

Please format your code by using the </> button when editing a message on the forum or use backticks ``` code ``` → code (specially in Python indentations are important)

What do you mean by generator? Do you mean simplify the code you currently have so it can give the astrological sign by giving it a day and a month?

hello, thanks for responding! so im supposed to generate identicons (illustrations I’ve made myself) based on the information you provide the code at the beginning. so the code i have here only provides an output on the console but not the actual display window, so how would i possibly relate the two?

hello, thank you for responding!
so the actual code itself is fine, but i just have to somehow use this code to create an output that will display an image on the window (this image i will make and import into the code) and im not sure how to do that

To load an image instead of display some text, you can use an image() function:

day = 23
month = 'december'

if month == 'december':
    x = 'sagittarius.png' if (day < 22) else 'capricorn.png'
...

image(loadImage(x), 0, 0)

Note that the x value is now assigned to a file name (with a .png extension).

2 Likes

hello! thanks this answer helped out!

but if I want to add multiple images (that were images of the different astrology signs) how would i correlate that set of code with the else if statements; so, my final goal would be: i insert my date of birth into program, and the output would give one of the 12 images (because 12 astrology signs)

i really appreciate your help :slight_smile:

That’s just what he did with the previous code.

depending on the if’s different image names are assigned to x.

At the end, ONE image is loaded with that name

1 Like

@anon, is this a Python course you are taking wherein a portion of your work is based on using Processing in Python Mode? If it is a Python course, have you learned about lists, dictionaries, and functions? It does seem that you have already learned about conditional blocks and ternary conditional statements, but long chains of conditional blocks can get messy.

If the answer to the above questions is “yes”, you could use some of what you have learned to organize your code.

See the following functions, which use the Python language features mentioned above to get the astrological sign and the image file name from the month and day:

def astrological_sign(month, day):
  signs = {"January":["Capricorn", 20, "Aquarius"],
           "February": ["Aquarius", 19, "Pisces"],
           "March": ["Pisces", 21, "Aries"],
           "April": ["Aries", 20, "Taurus"],
           "May": ["Taurus", 21, "Gemini"],
           "June": ["Gemini", 21, "Cancer"],
           "July": ["Cancer", 23, "Leo"],
           "August": ["Leo", 23, "Virgo"],
           "September": ["Virgo", 23, "Libra"],
           "October": ["Libra", 23, "Scorpio"],
           "November": ["Scorpio", 22, "Sagittarius"],
           "December": ["Sagittarius", 22, "Capricorn"]
           }
  return signs[month][0] if day < signs[month][1] else signs[month][2]

def image_name(month, day):
  return astrological_sign(month, day).lower() + ".png"

print(astrological_sign("January", 14))
print(image_name("January", 14))

Output:

Capricorn
capricorn.png

If you use the above as a model, please check the dates of the astrological signs, and make sure the image_name function gives us correct file names consistent with your work. Modify as necessary.

2 Likes

hello!

so i’ve tried out the code you’ve given, and to answer your question, yes i have learned about lists, dictionaries, etc. i tried to integrate your code with mine and i keep getting a syntax error, so im not sure what exactly im doing wrong.

size(800,800)

username = 'anon'
day = 20
month = 'january'

def astrological sign(month, day):
  signs = {"January":["Capricorn", 20, "Aquarius"],
           "February": ["Aquarius", 19, "Pisces"],
           "March": ["Pisces", 21, "Aries"],
           "April": ["Aries", 20, "Taurus"],
           "May": ["Taurus", 21, "Gemini"],
           "June": ["Gemini", 21, "Cancer"],
           "July": ["Cancer", 23, "Leo"],
           "August": ["Leo", 23, "Virgo"],
           "September": ["Virgo", 23, "Libra"],
           "October": ["Libra", 23, "Scorpio"],
           "November": ["Scorpio", 22, "Sagittarius"],
           "December": ["Sagittarius", 22, "Capricorn"]
           }
  return signs[month][0] if day < signs[month][1] else signs[month][2]

def image name(month, day):
  return astrological sign(month, day).lower() + 'aries.png'


for i in range(1000):
    point( random(width), random(height) )
fill('#E36E6E')   
noStroke() 
ellipse(200,200, 40,35)
fill('#337755')
ellipse(400,500, 20,20)

1 Like

The code did not copy correctly. In several places, you have a space where there needs to be an underscore. For example you have this:

def astrological sign(month, day):

Instead you should have this:

def astrological_sign(month, day):

Check all your code in the editor for this type of SyntaxError.

The term generator has several meanings. For example, there is a feature of Python known as a generator that relates to iteration. I don’t think that is what you need for this project, but please let us know if that is what you have been asked to create.

1 Like

hello,

so i’ve made the changes to the code and it works, so my output is the ellipse that i’ve drawn and the random “stars,” but the image i have put into the code does not come up, is there any reason why?
so my course has only focused on the very basics of python, so we have learnt iterations, data visualization, else and if functions, so im assuming they want me to create a generator that will produce 20 different unique outcomes with simple code. so is there any way to use else if statements (like the one i used before) to create a generator that will have an output of different images based on the day and month i put in?

I don’t see any place in your code where you render an image on the screen. See the following, particularly the second and third items:

EDITS (March 29, 2021):

Yes, the conditional structure that you posted earlier can be used. Just adapt it based on information that you find in the Processing.py Reference regarding loading and displaying images.

If you use the functions above, you can do this to load an image:

img = loadImage(image_name("January", 14))
1 Like

so the code you’ve given, wouldn’t i have to continually change the month and date? and the conditional structure i used could only print out the answer on the console, but i wasn’t sure of how i could add the image function to that. sorry this isn’t really wrapping around my brain :confused: i really appreciate your help tho thank you so much

Let’s consider @tabreturn’s example:

day = 23
month = 'december'

if month == 'december':
    x = 'sagittarius.png' if (day < 22) else 'capricorn.png'

Later on you would have something like:

img = loadImage(x)
# ... (other lines of code) ...
image(img, 50, 200)

That depends upon how you were asked to supply that information to the program.

hi,

so i’ve tried this out and i seem to get only one image all the time, no matter what value i put into the date or month. i think this is because python works in order of the code? so it’s only displaying the aries image because that was on top of the taurus image. is there any way to fix that?

size(800,800)

username = 'anon'
day = 30
month = 'may'

if month == 'april':
    x = 'aries1.png' if (day < 22) else 'taurus1.png'

image(loadImage('aries1.png'), 0, 0)
image(loadImage('taurus1.png'), 0, 0)

for i in range(1000):
    point( random(width), random(height) )
    

fill('#E36E6E')   
noStroke() 
ellipse(200,200, 40,35)
fill('#337755')
ellipse(400,500, 20,20)

Assuming you have filled in the entire conditional structure with all the appropriate month and day combinations, x will represent the name of the file you need. Then you can do something like this:

img = loadImage(x)
image(img, 20, 300)

so i have added more images to my code (astrology is not actually in order i just wanted to see whether it work)

username = 'anon'
day = 15
month = 'april'

if month == 'april':
    x = 'aries1.png' if (day < 22) else 'taurus1.png'
if month == 'may':
    x = 'leo1.png' if (day < 21) else 'virgo1.png'

img = loadImage('aries1.png')
image(img, 20, 50)
img = loadImage('taurus1.png')
image(img, 20, 50)
img = loadImage('leo1.png')
image(img, 20, 50)
img = loadImage('virgo1.png')
image(img, 20, 50)

for i in range(1000):
    point( random(width), random(height) )
    

fill('#E36E6E')   
noStroke() 
ellipse(200,200, 40,35)
fill('#337755')
ellipse(400,500, 20,20)

and down below is the error i got and im not sure what it means, does this mean i’ll have to add or change my code somehow?

NullPointerException
at processing.core.PGraphics.image(PGraphics.java:3814)
at processing.core.PApplet.image(PApplet.java:12813)
at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:188)
at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:206)
at org.python.core.PyObject.call(PyObject.java:534)
at org.python.core.PyObject.call(PyObject.java:540)
at org.python.core.PyMethod.call(PyMethod.java:171)
at org.python.pycode._pyx101.f$0(sketch_210330a.pyde:29)
at org.python.pycode._pyx101.call_function(sketch_210330a.pyde)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1614)
at org.python.core.Py.exec(Py.java:1658)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
at jycessing.PAppletJythonDriver.setup(Unknown Source)
at processing.core.PApplet.handleDraw(PApplet.java:2432)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)

Be sure that all the files are in the same folder or directory as your Python code. Also make sure that the names of the actual files are exactly the same as the names listed in your code.

Note that this will place each image on top of the previous one, and you will only see the final image:

img = loadImage('aries1.png')
image(img, 20, 50)
img = loadImage('taurus1.png')
image(img, 20, 50)
img = loadImage('leo1.png')
image(img, 20, 50)
img = loadImage('virgo1.png')
image(img, 20, 50)

Replace all of that with this:

img = loadImage(x)
image(img, 20, 50)

Then the value of x will determine which image is loaded and displayed.

EDIT (March 29, 2021):

Based on the above code, the variable img should serve as a reference, or pointer, to an image file. However, if the file with the appropriate name is not there, the loadImage() function will have failed to set up the pointer correctly. As a result, we will get a NullPointerException when we attempt to display the image.

1 Like

thank you so much, the i managed to make it work!! i really appreciate your help :slight_smile:

1 Like