CREATE YOUR AI CUM SLUT ON CANDY.AI TRY FOR FREE
x

Ren'Py Help with converting alphabet letters to their numbers and using in Lists [SOLVED]

Turning Tricks

Rendering Fantasies
Game Developer
Apr 9, 2022
1,355
2,523
I have a rather strange task I need to learn how to do in Ren'py.

What I need is to convert any alphabet letters to a number and then use that number to extract a result from a List.

So what I am making has a List that has 26 separate strings in it like this example...

$ topics = ["string1", "string2", "string3", "string4", "string5", "string6", ... "string26"]

Now I am going to have the player input a string in answer to a quiz, and I will be extracting a single letter from that string.

example:

Python:
e "Do you like Dogs or Cats?"

$ answer = renpy.input("Type your answer...?")

$ answer = answer.strip()

$ answer = answer.capitalize()


e "So you like [answer]? Cool!"
I then want to take the first letter of that string...

$ first_letter = answer[0]

Now here's where I get lost. I need to convert [first_letter] to it's numeric value in the alphabet (either 0-25 or 1-26) and then use that on my topics List above to return the proper string. So, for example, if "A" was the first letter then I would want to convert that to 0 or 1 and then use it to return the proper string.
$ result = topics[0]

I've looked all over and have found several examples of using the ord (ordinal) command in Python but I can't get that to work within Ren'py. I'm also hitting a wall using a str to get an int and then use that on the list.

Any help would be greatly appreciated!

Thx
 

GetOutOfMyLab

Conversation Conqueror
Modder
Aug 13, 2021
7,404
20,368
I have a rather strange task I need to learn how to do in Ren'py.

What I need is to convert any alphabet letters to a number and then use that number to extract a result from a List.

So what I am making has a List that has 26 separate strings in it like this example...

$ topics = ["string1", "string2", "string3", "string4", "string5", "string6", ... "string26"]

Now I am going to have the player input a string in answer to a quiz, and I will be extracting a single letter from that string.

example:

Python:
e "Do you like Dogs or Cats?"

$ answer = renpy.input("Type your answer...?")

$ answer = answer.strip()

$ answer = answer.capitalize()


e "So you like [answer]? Cool!"
I then want to take the first letter of that string...

$ first_letter = answer[0]

Now here's where I get lost. I need to convert [first_letter] to it's numeric value in the alphabet (either 0-25 or 1-26) and then use that on my topics List above to return the proper string. So, for example, if "A" was the first letter then I would want to convert that to 0 or 1 and then use it to return the proper string.
$ result = topics[0]

I've looked all over and have found several examples of using the ord (ordinal) command in Python but I can't get that to work within Ren'py. I'm also hitting a wall using a str to get an int and then use that on the list.

Any help would be greatly appreciated!

Thx
You could use the python ord() function.

Python:
def letter_to_number(letter):    
    return ord(letter.lower()) - ord('a') + 1
 

Winterfire

Forum Fanatic
Respected User
Game Developer
Sep 27, 2018
5,527
8,062
Python:
label start:
    e "Do you like Dogs or Cats?"
    $answer = renpy.input("Type your answer...?")
    $answer = answer.strip()
    $answer = answer.capitalize()
    e "So you like [answer]? Cool!"
    $startLetter = answer[0].upper()
    
    #https://media.geeksforgeeks.org/wp-content/uploads/20240304094301/ASCII-Table.png
    #Always -65 to get the index starting from 0 (65-65=0, 66-65=1, 67-65=2)
    $index = ord(startLetter) - 65
    
    #failsafe to avoid index below 0 or more than 25
    if index<0 or index>25:
        return
    else:
        $selectedTopic = topics[index]
        e "Your topic is: [selectedTopic]"
 

Turning Tricks

Rendering Fantasies
Game Developer
Apr 9, 2022
1,355
2,523
You guys rock!

You could use the python ord() function.

Python:
def letter_to_number(letter): 
    return ord(letter.lower()) - ord('a') + 1
I had some problems with this, but I think it was just my lack of understanding the syntax for using this inside Ren'py

Python:
label start:
    e "Do you like Dogs or Cats?"
    $answer = renpy.input("Type your answer...?")
    $answer = answer.strip()
    $answer = answer.capitalize()
    e "So you like [answer]? Cool!"
    $startLetter = answer[0].upper()
 
    #https://media.geeksforgeeks.org/wp-content/uploads/20240304094301/ASCII-Table.png
    #Always -65 to get the index starting from 0 (65-65=0, 66-65=1, 67-65=2)
    $index = ord(startLetter) - 65
 
    #failsafe to avoid index below 0 or more than 25
    if index<0 or index>25:
        return
    else:
        $selectedTopic = topics[index]
        e "Your topic is: [selectedTopic]"
This worked like a charm Winterfire, thank you very much!
 
  • Like
Reactions: Winterfire