-2

I'm trying to understand how the whole thing in Python works. I want to ask the user for "ID" and then to read from a .py file with the "ID" name.

for example: the user insert "361" and the code will open "361.py" and get variables from that file

1 Answers1

0

Mainfile:

import ID_361
a = input('Enter Code: ')
if a  =='361':
    ID_361.ID_361()

ID_361:

def ID_361():
    print('OK')

The files have to be in the same directory!

But can you say for what you need it? It gives way more easier ways

Better:

Mainfile:

import IDs
a = input('Enter Code: ')
if a  =='361':
    IDs.ID_361()
elif a  =='362':
    IDs.ID_362()
elif a  =='363':
    IDs.ID_363()

IDs:

def ID_361():
    print('OK 361')
def ID_362():
    print('OK 362')
def ID_363():
    print('OK 363')

Pros: You only have 2 files and not like 100 which makes it clear

chraebsli
  • 184
  • 12
  • well, if there are 1000 IDs it makes it a lot harder haha. my thinking went to something like `id = input("what is your ID?") import id` which obviously doesn't work – Benny Kerido Feb 18 '21 at 12:52
  • I'm trying to create a kind of "users" system and give each user different parameters. then if for example I want to get the user's age I just go to his file and read `age` variable – Benny Kerido Feb 18 '21 at 12:56
  • @BennyKerido yeah I know what you mean, but you have to use a function. Then it doesn't matter if you have all definitions in one file or for each definition in a single file. What you can do if you only want to use it like a database you can write the data in a file (which is complicated) or do it with a dictionary like this `database={'id1':{id1},'id2':{id2}}` and with the user ids like this `id1={'age':21,'name':'user1234'} ` Help: [w3schools.com](https://www.w3schools.com/python/python_dictionaries.asp) I hope I can help you with that – chraebsli Feb 21 '21 at 21:15