Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#1
Post
by namastaii » Wed Apr 20, 2016 9:03 pm
Okay so I give up and come here because I've been searching the web for this and I can only find information on how to find out if there is a specific value in a list.
How do I just simply make an 'if' statement that says if there is two items in a list then do something?
I'm basically trying to do something like this:
Code: Select all
if my_list[0]:
text "[my_list[0]]"
if my_list[1]:
text "[my_list[1]]"
I just apparently can't figure out the right way to format this
because without the 'if' statements then I will get an index out of range error obviously if the list doesn't have enough in it so I want it to go off of what is available because it will change a lot
Last edited by
namastaii on Sat Apr 23, 2016 5:32 pm, edited 1 time in total.
-
Zetsubou
- Miko-Class Veteran
- Posts: 513
- Joined: Wed Mar 05, 2014 1:00 am
- Completed: See my signature
- Github: koroshiya
- itch: zetsuboushita
-
Contact:
#2
Post
by Zetsubou » Wed Apr 20, 2016 11:22 pm
If you're checking the length of the list, you want len().
Code: Select all
if len(myList) == 1:
"One item in the list"
elif len(myList) == 2:
"Two items in the list"
If you're checking for a specific value in a list, use "in".
Code: Select all
if "mystring" in myList:
"Yep, it's in there."
else:
"...Nope."
myList[0] would return the first value in the list, myList[1] the second, and so on.
Like that, you're accessing values at different indexes.
So len() for length, [index] for actually accessing a value.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails
Working on:
Sable's Grimoire 2

-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#3
Post
by namastaii » Thu Apr 21, 2016 12:04 am
I tried len and it didn't work.. I will look at it again. Maybe I phrased it differently. Seems pretty straight forward to me.
-
trooper6
- Lemma-Class Veteran
- Posts: 3712
- Joined: Sat Jul 09, 2011 10:33 pm
- Projects: A Close Shave
- Location: Medford, MA
-
Contact:
#4
Post
by trooper6 » Thu Apr 21, 2016 12:17 am
Do you just want a for loop?
You can see how I use a for loop in an inventory screen in this thread:
viewtopic.php?f=8&t=30788
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe:
http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
-
PyTom
- Ren'Py Creator
- Posts: 15893
- Joined: Mon Feb 02, 2004 10:58 am
- Completed: Moonlight Walks
- Projects: Ren'Py
- IRC Nick: renpytom
- Github: renpytom
- itch: renpytom
- Location: Kings Park, NY
-
Contact:
#5
Post
by PyTom » Thu Apr 21, 2016 12:24 am
Do you mean, is a particular item in the list twice? If so, you probably want to use a list comprehension. List comprehensions are a way to make another list from your list. You can then take the len from that second list. So you can write:
Code: Select all
if len([i for i in myList if i == "banana" ]) >= 2:
"There are at least two bananas in the list. That's practically a bunch!"
If this isn't it, we might need an example of what you're trying to do.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama •
https://www.patreon.com/renpytom
-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#6
Post
by namastaii » Thu Apr 21, 2016 12:36 am
trooper6 wrote:Do you just want a for loop?
You can see how I use a for loop in an inventory screen in this thread:
viewtopic.php?f=8&t=30788
I know about the for loop but with my screen, I have it a certain way so I'm doing them individually instead of listing them all in one place.
PyTom wrote:Do you mean, is a particular item in the list twice? If so, you probably want to use a list comprehension. List comprehensions are a way to make another list from your list. You can then take the len from that second list. So you can write:
Code: Select all
if len([i for i in myList if i == "banana" ]) >= 2:
"There are at least two bananas in the list. That's practically a bunch!"
If this isn't it, we might need an example of what you're trying to do.
No, these aren't items or anything they're just phrases that are going to be grouped up and I don't need to know of any duplicates. I just need to ... I have a screen that lists each thing in the list. I know a for loop can do this but I just want to do each item separately because of the theme I have. I put each one in an hbox with other elements is why. I just want some if statements where if there is one item, then list it, if there are two then list the second one too, if there isn't a third then don't list it but that spot is available once there is a third one. But to stay within my theme. There is probably a 'better' way to do this but I like the way I'm doing it. I will try out the first example that was posted, I'm just away from a computer until tomorrow.
-
Onishion
- Veteran
- Posts: 295
- Joined: Mon Apr 20, 2015 10:36 am
-
Contact:
#7
Post
by Onishion » Thu Apr 21, 2016 1:05 am
Well, the problem with how you originally did it is, if there is no "my_list[4]" or whatever, then you get out of index errors for even looking. len should work, but remember that len counts the total number of things, but the list index always starts at zero, so if len spits back that there are "3 things in the list," then those three things are at locations 0, 1, and 2, not 1, 2, and 3, and looking for "my_list[3]" will throw back errors.
-
trooper6
- Lemma-Class Veteran
- Posts: 3712
- Joined: Sat Jul 09, 2011 10:33 pm
- Projects: A Close Shave
- Location: Medford, MA
-
Contact:
#8
Post
by trooper6 » Thu Apr 21, 2016 1:30 am
You can put that hbox inside of the for loop.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe:
http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#9
Post
by namastaii » Thu Apr 21, 2016 2:46 am
I know it starts at 0...
I don't really know how to explain what I'm doing XD
Okay I'll be more specific.
I have a list of daily tasks. The daily task list will change depending on factors in the story and choices the character makes. There is a little list screen in the game that shows the current tasks. The list will obviously not always be the same length. I'm just putting down the listed tasks on the screen but I am putting them on a bulleted list and I didn't want to look into anything too complex to code this so I just created separate hboxs on the screen that add an image of a bullet before each task (task[0], task[1], etc)
-
trooper6
- Lemma-Class Veteran
- Posts: 3712
- Joined: Sat Jul 09, 2011 10:33 pm
- Projects: A Close Shave
- Location: Medford, MA
-
Contact:
#10
Post
by trooper6 » Thu Apr 21, 2016 2:52 am
namastaii wrote:I know it starts at 0...
I don't really know how to explain what I'm doing XD
Okay I'll be more specific.
I have a list of daily tasks. The daily task list will change depending on factors in the story and choices the character makes. There is a little list screen in the game that shows the current tasks. The list will obviously not always be the same length. I'm just putting down the listed tasks on the screen but I am putting them on a bulleted list and I didn't want to look into anything too complex to code this so I just created separate hboxs on the screen that add an image of a bullet before each task (task[0], task[1], etc)
Yeah, like I said, you can still use the for loop.
Something like:
Code: Select all
screen todo_screen():
vbox:
text "To Do List:"
for item in my_list:
hbox:
add "bulletPoint.png"
text ("[item]")
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe:
http://lemmasoft.renai.us/forums/viewto ... 51&t=21978
-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#11
Post
by namastaii » Thu Apr 21, 2016 3:04 am
Okay, I will try that when I can Thank you
-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#12
Post
by namastaii » Thu Apr 21, 2016 11:25 pm
It works perfectly, Thanks
-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#13
Post
by namastaii » Fri Apr 22, 2016 12:16 am
I am however doing something wrong on my else statement. It gets ignored completely. I've tried rephrasing the if statements but even though those don't seem to have a problem.. else won't work. If I empty the list, it still doesn't work. If I start the game and don't add anything to the list, it doesn't work.
I just want it to say 'no tasks' when the list is empty
Code: Select all
frame style style.frame['black']:
vbox:
if len(task_complete) >=0:
for item in task_complete:
hbox:
image "ui/bullet.png" ypos 10
text " "
text "[item]" style "taskcomplete_text"
if len(task) >=0:
for item in task:
hbox:
image "ui/bullet.png" ypos 10
text " "
text "[item]" style "tasklist_text"
else:
hbox:
text "No tasks." style "tasklist_text"
-
Zetsubou
- Miko-Class Veteran
- Posts: 513
- Joined: Wed Mar 05, 2014 1:00 am
- Completed: See my signature
- Github: koroshiya
- itch: zetsuboushita
-
Contact:
#14
Post
by Zetsubou » Fri Apr 22, 2016 1:13 am
I just want it to say 'no tasks' when the list is empty
Then stop using >= 0.
len() of an empty list is 0, so it will match your if statement.
In other words, change your >= to >.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails
Working on:
Sable's Grimoire 2

-
namastaii
- Eileen-Class Veteran
- Posts: 1350
- Joined: Mon Feb 02, 2015 8:35 pm
- Projects: Template Maker for Ren'Py, What Life
- Github: lunalucid
- Skype: Discord: lunalucid#1991
- Soundcloud: LunaLucidMusic
- itch: lunalucid
- Location: USA
-
Contact:
#15
Post
by namastaii » Fri Apr 22, 2016 1:19 am
Oh I did. that just happened to be the last thing I tried. I tried > 0 and > -1 etc