Python - efficient way to create 20 variables?

2024/7/7 23:41:41

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the variables as needed when they are needed because I also have some if/else statements that need to check whether the variables are still empty or already equal to other strings.

Instead of writing

variable_a = ''
variable_b = ''
....

I thought at something like

list = ['a', 'b']
for item in list:exec("'variable_'+item+' = '''")

This code does not lead to an error, but still is does not do what I would expect - the variables are not created with the names "variable_1" and so on.

Where is my mistake?

Thanks, Woodpicker

Answer

Where is my mistake?

There are possibly three mistakes. The first is that 'variable_' + 'a' obviously isn't equal to 'variable_1'. The second is the quoting in the argument to exec. Do

for x in list:exec("variable_%s = ''" % x)

to get variable_a etc.

The third mistake is that you're not using a list or dict for this. Just do

variable = dict((x, '') for x in list)

then get the contents of "variable" a with variable['a']. Don't fight the language. Use it.

https://en.xdnf.cn/q/119566.html

Related Q&A

Whatsapp asking for updating chrome version

I am trying to open whatsapp with selenium and python, it was working fine until today. In headless or non, whatsapp is now asking to update chrome, when I try to do so, Chrome throws this error: An er…

how to find the longest N words from a list, using python?

I am now studying Python, and I am trying to solve the following exercise:Assuming there is a list of words in a text file, My goal is to print the longest N words in this list.Where there are several …

([False, True] and [True, True]) evaluates to [True, True]

I have observed the following behavior in python 3: >>> ([False, True] and [True, True]) [True, True]>>> ([False, True] or [True, True]) [False, True]I was expecting exactly the oppos…

Uploading an image to Flask server

I am struggling bit with Flask and uploading a file, here is my Flask code so far:@app.route(/api/user/update/, methods=[PUT]) @auth.login_required def update_user():# check if the post request has the…

Enemy Projectiles Arent Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles arent showing on my screen VIDEO. He isnt attacking at all, I dont know why. I am in my main loop I draw t…

Python+Selenium. Cant locate element

Ive implemented the script using Python and selenium to click on the ads. But now this script is not working.Unable to find element on the page.Please help me to correct the script. Thank you!from sele…

AttributeError: NoneType object has no attribute channels [duplicate]

This question already has answers here:Why do I get AttributeError: NoneType object has no attribute something?(11 answers)Closed 5 years ago.Hi Im having an issue with a module for my Discord bot. Im…

How to get a specific value from a html header

Im using selenium to get request headers from a web page, the problem is that it prints out all request headers sent and i want to get only one value from one of them. I dont know how to do it and i ha…

How to use windows as raspberry pi and connect the windows with another raspberry pi [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Python : Return the missing weekdays dates and assign rate next to missing date

Dates rates 7/26/2019 1.04 7/30/2019 1.0116 7/31/2019 1.005 8/1/2019 1.035 8/2/2019 1.01 8/6/2019 0.9886 8/7/2019 1.0048 8/8/2019 0.97 8/9/2019 0.9659 8/12/2019 0.965In …