[SOLVED].lower() function error

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.
Post Reply
Message
Author
User avatar
MrTorex
Newbie
Posts: 7
Joined: Fri Jan 25, 2019 3:00 am
Projects: Secret Idea
Location: Belarus
Contact:

[SOLVED].lower() function error

#1 Post by MrTorex »

Hello everyone!
I'm new in Ren'py and in Python too.
I have a question.
I wrote a function in code that is responsible for viewing a Windows username. To check whether the username is the same with the name that the player enters at the beginning of the game, I created new variables that are equal to the computer name.lower () and player name.lower (). There is no error with the player’s name, this function does not want to work with the computer’s name. This error occurs in Ren'py, there are no errors in the Python 3 console. Help me please.
Code:
python:
name = renpy.input("Как вас зовут?" (translation "What is your name?")
name = name.strip() or ("NONAME")
import os
user = os.environ.get('username')
userl = user.lower()
namel = name.lower()
q "[userl], [namel]."
I can't insert images, so here are the links to them:
https://prntscr.com/mby8t8
https://prntscr.com/mby983
https://prntscr.com/mbyj7d
Already thanks.
(Yes, I'm from Russian speaking country)
Last edited by MrTorex on Sat Jan 26, 2019 11:49 am, edited 1 time in total.

User avatar
SONTSE
Regular
Posts: 96
Joined: Sun Nov 24, 2013 10:49 pm
Completed: 11 VN's so far
Discord: jkx0282_10798
Contact:

Re: .lower() function error

#2 Post by SONTSE »

you need

Code: Select all

        import os
        import pwd
        user = pwd.getpwuid( os.getuid() )[ 0 ]
instead of

Code: Select all

    import os
    user = os.environ.get('username')

User avatar
MrTorex
Newbie
Posts: 7
Joined: Fri Jan 25, 2019 3:00 am
Projects: Secret Idea
Location: Belarus
Contact:

Re: .lower() function error

#3 Post by MrTorex »

Lena_Borodach wrote: Fri Jan 25, 2019 6:22 am you need

Code: Select all

        import os
        import pwd
        user = pwd.getpwuid( os.getuid() )[ 0 ]
instead of

Code: Select all

    import os
    user = os.environ.get('username')

Code: Select all

	IMPORT ERROR:
	No module named pwd

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: .lower() function error

#4 Post by Imperf3kt »

I use the following code to assign the windows login name with a variable. You could use the same and then compare the input name, with the name this code reports?

Code: Select all

init python:
    if os.name == 'nt':
        import os
        
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get(name)
    elif os.name == 'posix':
        import os
        
        for user in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get('USER')
The nt section is for Windows environment. posix is for Linux. You must put this somewhere before your start label. I suggest either in its own file, or in screens.rpy

The name is assigned to the variable "player"
Here's an example usage. This code is part of my main menu:

Code: Select all

    if gui.show_name:
        vbox:
            xalign 0.97
            yalign 0.04
            
            if not renpy.variant("android"):
                text "Welcome, [player]":
                    style "main_menu_version"
            else:
                null height 2
                
            text "[config.name!t]":
                style "main_menu_title"

            text "Version [config.version]":
                style "main_menu_version"
                
Image
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
MrTorex
Newbie
Posts: 7
Joined: Fri Jan 25, 2019 3:00 am
Projects: Secret Idea
Location: Belarus
Contact:

Re: .lower() function error

#5 Post by MrTorex »

Imperf3kt wrote: Fri Jan 25, 2019 6:43 am I use the following code to assign the windows login name with a variable. You could use the same and then compare the input name, with the name this code reports?

Code: Select all

init python:
    if os.name == 'nt':
        import os
        
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get(name)
    elif os.name == 'posix':
        import os
        
        for user in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            player = os.environ.get('USER')
The nt section is for Windows environment. posix is for Linux. You must put this somewhere before your start label. I suggest either in its own file, or in screens.rpy

The name is assigned to the variable "player"
Here's an example usage. This code is part of my main menu:

Code: Select all

    if gui.show_name:
        vbox:
            xalign 0.97
            yalign 0.04
            
            if not renpy.variant("android"):
                text "Welcome, [player]":
                    style "main_menu_version"
            else:
                null height 2
                
            text "[config.name!t]":
                style "main_menu_title"

            text "Version [config.version]":
                style "main_menu_version"
                
Image
Thank you, but this code also does not work with .lower ().
And now I don't know what do with my error. And thank you for telling me the option that will work with Linux.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: .lower() function error

#6 Post by trooper6 »

Quick note for future reference: RenPy uses Python 2.7 not Python 3.
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

User avatar
SONTSE
Regular
Posts: 96
Joined: Sun Nov 24, 2013 10:49 pm
Completed: 11 VN's so far
Discord: jkx0282_10798
Contact:

Re: .lower() function error

#7 Post by SONTSE »

really confused on this issue, because both mine and Imperf3kt's code do work in my case. don't you mind for some sanity checks?
what is your os?
what is your renpy version?
there is a project with Imperf3kt's code implementation attached in a zip file below. can you run it? how does it wurk?
Attachments
_os_name.zip
(68.85 KiB) Downloaded 12 times

User avatar
MrTorex
Newbie
Posts: 7
Joined: Fri Jan 25, 2019 3:00 am
Projects: Secret Idea
Location: Belarus
Contact:

Re: .lower() function error

#8 Post by MrTorex »

Lena_Borodach wrote: Fri Jan 25, 2019 2:52 pm really confused on this issue, because both mine and Imperf3kt's code do work in my case. don't you mind for some sanity checks?
what is your os?
what is your renpy version?
there is a project with Imperf3kt's code implementation attached in a zip file below. can you run it? how does it wurk?
No results
OS - Windows 7 Maximum
Ren'py v. 7.1.3.1092
My code is next:

Code: Select all

python:
        name = renpy.input("Как вас зовут?") #ENG: "What is your name?"
        name = name.strip() or ("NONAME")
        import os
        user = os.environ.get('username')
And your program prints to me that my name in lower is ЛешаЦ. IDK.
https://prntscr.com/mcfh96

User avatar
SONTSE
Regular
Posts: 96
Joined: Sun Nov 24, 2013 10:49 pm
Completed: 11 VN's so far
Discord: jkx0282_10798
Contact:

Re: .lower() function error

#9 Post by SONTSE »

just grabbed some random advice from internets
let's see if it work

Code: Select all

label start:
    python:
        import os
        name = renpy.input("Как вас зовут?") #ENG: "What is your name?"
        name = name.strip() or ("NONAME")
        namel = name.lower()
        nameu = name.upper()
        user = os.environ.get('username')
        userl = user.lower()
        useru = user.upper()
    
        userdec1 = user.decode('utf-8')
        userdec1l = userdec1.lower()
        userdec1u = userdec1.upper()
        userdec2 = unicode(user, 'utf-8')
        userdec2l = userdec2.lower()
        userdec2u = userdec2.upper()
        userdec3 = u'%s'%user
        userdec3l = userdec3.lower()
        userdec3u = userdec3.upper()
    '[name] [namel] [nameu] \n [user] [userl] [useru] \n [userdec1] [userdec1l] [userdec1u] \n [userdec2] [userdec2l] [userdec2u] \n [userdec3] [userdec3l] [userdec3u] \n '
    return
and sorry about sanity checks - it turs out insane one is me and os.environ.get('username') returns None in macos witch of course cannot be lowered ^^

User avatar
MrTorex
Newbie
Posts: 7
Joined: Fri Jan 25, 2019 3:00 am
Projects: Secret Idea
Location: Belarus
Contact:

Re: .lower() function error

#10 Post by MrTorex »

Lena_Borodach wrote: Sat Jan 26, 2019 9:18 am just grabbed some random advice from internets
let's see if it work

Code: Select all

label start:
    python:
        import os
        name = renpy.input("Как вас зовут?") #ENG: "What is your name?"
        name = name.strip() or ("NONAME")
        namel = name.lower()
        nameu = name.upper()
        user = os.environ.get('username')
        userl = user.lower()
        useru = user.upper()
    
        userdec1 = user.decode('utf-8')
        userdec1l = userdec1.lower()
        userdec1u = userdec1.upper()
        userdec2 = unicode(user, 'utf-8')
        userdec2l = userdec2.lower()
        userdec2u = userdec2.upper()
        userdec3 = u'%s'%user
        userdec3l = userdec3.lower()
        userdec3u = userdec3.upper()
    '[name] [namel] [nameu] \n [user] [userl] [useru] \n [userdec1] [userdec1l] [userdec1u] \n [userdec2] [userdec2l] [userdec2u] \n [userdec3] [userdec3l] [userdec3u] \n '
    return
and sorry about sanity checks - it turs out insane one is me and os.environ.get('username') returns None in macos witch of course cannot be lowered ^^
If with your code:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 13, in script
    python:
  File "game/script.rpy", line 23, in <module>
    userdec1 = user.decode('utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 0: invalid continuation byte

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 13, in script
    python:
  File "E:\renpy-7.1.3-sdk\renpy\ast.py", line 881, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "E:\renpy-7.1.3-sdk\renpy\python.py", line 1913, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 23, in <module>
    userdec1 = user.decode('utf-8')
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/encodings/utf_8.py", line 16, in decode
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 0: invalid continuation byte

Windows-7-6.1.7601-SP1
Ren'Py 7.2.0.235
Test lower 1.0
Sat Jan 26 17:11:09 2019
And if I comment on lines that cause an error

Code: Select all

label start:
    python:
        import os
        name = renpy.input("Как вас зовут?") #ENG: "What is your name?"
        name = name.strip() or ("NONAME")
        namel = name.lower()
        nameu = name.upper()
        user = os.environ.get('username')
        userl = user.lower()
        useru = user.upper()

        #userdec1 = user.decode('utf-8')
        #userdec1l = userdec1.lower()
        #userdec1u = userdec1.upper()
        #userdec2 = unicode(user, 'utf-8')
        #userdec2l = userdec2.lower()
        #userdec2u = userdec2.upper()
        #userdec3 = u'%s'%user
        #userdec3l = userdec3.lower()
        #userdec3u = userdec3.upper()
    '[name] [namel] [nameu] \n [user] [userl] [useru]' #\n [userdec1] [userdec1l] [userdec1u] \n [userdec2] [userdec2l] [userdec2u] \n [userdec3] [userdec3l] [userdec3u] \n '
return
This doesn't works.
https://prnt.sc/mcgyh4

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: .lower() function error

#11 Post by IrinaLazareva »

try

Code: Select all

label start:
    '?'
    python:
        import os
        user = '%s' % os.environ.get('username')
        user = user.lower()
    '[user]'
    return

User avatar
MrTorex
Newbie
Posts: 7
Joined: Fri Jan 25, 2019 3:00 am
Projects: Secret Idea
Location: Belarus
Contact:

Re: .lower() function error

#12 Post by MrTorex »

IrinaLazareva wrote: Sat Jan 26, 2019 10:53 am try

Code: Select all

label start:
    '?'
    python:
        import os
        user = '%s' % os.environ.get('username')
        user = user.lower()
    '[user]'
    return
OH MY IT WORKS.
Thank you!

Post Reply

Who is online

Users browsing this forum: Google [Bot]