A
A
Agrael313 Zashefrovano2021-10-16 12:22:23
Python
Agrael313 Zashefrovano, 2021-10-16 12:22:23

How to simplify (optimize) the code in the voice assistant in pythone?

Hello, help me simplify the code.
I have a JSON file that stores answers to questions

dicts = open('body.json', 'r')
                    opening = json.load(dicts)
                    radiant0 = random.choice(opening["привет"])
                    radiant1 = random.choice(opening["как дела"])
                    value0 = radiant0
                    value1 = radiant1
                    alisa0 = 'echo '+value0+'|RHVoice-test -p anna'
                    subprocess.call(alisa0, shell=True)
                    alisa1 = 'echo ' + value1 + '|RHVoice-test -p anna'
                    subprocess.call(alisa1, shell=True)

Here's what you can do so that I don't write a lot of these variables like value0,1 and randiant0, 1
There will be a lot of questions in JSON, so don't write value0,1 and randiant0, 1 every time, what can you think of?
Please write if you don't mind

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-10-16
@Lazarusnode13

what can I do so that I don’t write a lot of these variables like value0,1 and randiant0

Just don't write them. They appeared in the code because you put them there.
If we compress as much as possible, then let
subprocess.call('echo '+random.choice(opening["привет"])+'|RHVoice-test -p anna', shell=True)
subprocess.call('echo '+random.choice(opening["как дела"])+'|RHVoice-test -p anna', shell=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question