# PLEASE HELP: creating a generator (astro signs)

**URL:** <https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933>\
**Category:** Processing.py\
**Tags:** homework\
**Created:** [March 28, 2021, 11:12pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933 "2021-03-28T23:12:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 28, 2021, 11:12pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/1 "2021-03-28T23:12:07Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 28, 2021, 11:36pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/2 "2021-03-28T23:36:44Z")

</div>

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

> [@anon](#):
>
> x = ‘Sagittarius’ if (day \< 22) else ‘capricorn’

Does this work??

Try

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

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 28, 2021, 11:38pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/3 "2021-03-28T23:38:51Z")

</div>

Hi,

Welcome to the forum! 😉

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)

> [@anon](#):
>
> im supposed to be creating a generator of some sort

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?

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 4:44am UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/4 "2021-03-29T04:44:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 4:46am UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/5 "2021-03-29T04:46:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [March 29, 2021, 5:23am UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/6 "2021-03-29T05:23:49Z")

</div>

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

```python
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).

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 3:41pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/7 "2021-03-29T15:41:17Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 29, 2021, 4:33pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/8 "2021-03-29T16:33:44Z")

</div>

> [@anon](#):
>
> 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

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

> [@tabreturn](#):
>
> Note that the `x` value is now assigned to a file name (with a `.png` extension).

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

At the end, ONE image is loaded with that name

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 5:55pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/9 "2021-03-29T17:55:26Z")

</div>

@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`:

```auto
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.

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 8:29pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/10 "2021-03-29T20:29:11Z")

</div>

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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 9:25pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/11 "2021-03-29T21:25:37Z")

</div>

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:

```auto
def astrological sign(month, day):

```

Instead you should have this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 9:45pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/12 "2021-03-29T21:45:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 9:54pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/13 "2021-03-29T21:54:36Z")

</div>

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:

- [Processing.py Reference](https://py.processing.org/reference/)
- [Processing.py Reference: `loadImage()`](https://py.processing.org/reference/loadImage.html)
- [Processing.py Reference: `image()`](https://py.processing.org/reference/image.html)

_EDITS (March 29, 2021):_

> [@anon](#):
>
> … 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?

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:

```auto
img = loadImage(image_name("January", 14))

```

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 10:10pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/14 "2021-03-29T22:10:40Z")

</div>

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 😕 i really appreciate your help tho thank you so much

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 10:20pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/15 "2021-03-29T22:20:26Z")

</div>

Let’s consider [@tabreturn’s example](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/6):

```auto
day = 23
month = 'december'

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

```

Later on you would have something like:

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

```

> [@anon](#):
>
> … wouldn’t i have to continually change the month and date?

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

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 10:34pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/16 "2021-03-29T22:34:36Z")

</div>

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?

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 10:54pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/17 "2021-03-29T22:54:02Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 29, 2021, 11:06pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/18 "2021-03-29T23:06:01Z")

</div>

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

```auto
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)

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 11:15pm UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/19 "2021-03-29T23:15:52Z")

</div>

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:

```auto
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:

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

```

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

_EDIT (March 29, 2021):_

> [@anon](#):
>
> `NullPointerException`

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.

---

<div class="post-metadata">

**Author:** ![anon](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@anon](https://discourse.processing.org/u/anon)\
**Post date:** [March 30, 2021, 10:00am UTC](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933/21 "2021-03-30T10:00:06Z")

</div>

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

[Next page](https://discourse.processing.org/t/please-help-creating-a-generator-astro-signs/28933.md?page=2)
