简体   繁体   中英

How to make python create variables, without the user creating the variables?

I am making a program in Python where you would enter an integer and then it would create integer many variables (if that makes sense). How would I make Python do this? EDIT: Sorry for a contradictory title! :)

Your question contradicts itself, you want to prompt the user to enter an integer value that would in return, create that many values available for use. Just a note on:

...make python create variables, without the user creating the variables?

You're creating the values using python, so indirectly, python is creating them, the user is entering how many values he/she wants using a python program, so indirectly, the user is also creating them.

With that being said, a list will suffice for this:

num_of_vars=int(input("Enter number of int variables you want to create:"))
vals=[0] * num_of_vars
print(vals)

The input() function prompts the user it's argument, ie , the string "Enter number of int variables you want to create." However, this needs to be treated as an integer value. So we explicitly convert it using int() .

The list vals is initialized with num_of_vars elements (input of user) all with the value of 0. So if we input 5 and print this list out, we get:

[0, 0, 0, 0, 0]

Python created variables without the user creating them.

However, if you actually mean to write the "variables" in code, then that's entirely different, and can be done with nothing more than the input() function, python input and output and a ton of boredom.

Hope this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM