[Python] determinateing if a keyword is a string [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am curently working on a text game in python abutt proggraming with python. And I want the player to be able to buy upgrades like ability to use while loops, import keyword etc.

    Suppose I have a file solution.py in dictionary x/y/z/ the file in python like this:

    file = open('x//y//z//solution.py','r')


    and write it to a variable like this

    filecontent = file.read()


    and then I check if the file contains 'while' keyword:

    if filecontent.__contains__('while'):
    #run the file, this is just an example so I do not have to write this code
    else:
    print('you used an invalid keywoord in your solution')


    and this code works partially but what if the solution.py is

    print('while')


    the code will print you 'used an invalid keyword in your solution' string even tho I did not

    how do I fix it

    My goal is to have some code that skips over string uses of the keyword,(and maybe also variable names)

    I did not try anything beacause this problem is to much for my problem solving skills.

    Please account for examples like:

    print('not xwhile not something')

    Continue reading...

Compartilhe esta Página