1

Instead of writing manually:

var1 = input("Whats  your number")
var2 = input("Whats  your number")
var3 = input("Whats  your number")
...
var100 = input("Whats  your number")

Can I use a for loop or something?

I tried but does not work:

for x in range(1,100):
    var = "var" + str(x)
    "var" + str(x) = input("Whats  your number")
    print("var" + str(x))

SyntaxError: can't assign to operator

BitByBit
  • 567
  • 4
  • 16
  • Have a look at what `eval` can do. – Efferalgan Oct 02 '16 at 08:12
  • Why do you want 100 separate variables? Put that stuff into a list! But you should also consider: most users will not enjoy having to type 100 different numbers into your program. – PM 2Ring Oct 02 '16 at 08:14
  • 1
    @Efferalgan Please do not recommend using `eval` to new programmers, especially when there are far better ways to do what they're trying to do. – PM 2Ring Oct 02 '16 at 08:15
  • @PM2Ring Actually, I only need 3 user inputs but I just exaggerated to make a point. In essence I am repeating myself and that's not good practice. – BitByBit Oct 02 '16 at 08:18
  • Here's a compact way to read in some integers and put them into a list: `nums = [int(input('Enter number {}: '.format(i))) for i in range(3)]`. However, it's better to use something a bit more sophisticated to read the data, so you can gracefully handle bad input (like strings that can't be converted to integers). Take a look at [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/4014959) for a variety of useful techniques. – PM 2Ring Oct 02 '16 at 08:25
  • @PM2Ring Yea that makes more sense. It's the closest thing I came to instead of creating automaticaly a number of variables. Can you tell me why python does not allow to create a number of vairables automaticlly. Thanks – BitByBit Oct 02 '16 at 08:43
  • Oh, Python _does_ allow you to automatically create a series of variables, but it's a _really_ bad idea. Such variables are hard to work with, and your code soon becomes a horrible unmaintainable mess. And even if you create a series of numbered variables by hand it's not a good idea. Instead of having a bunch of numbered variables, you should use a list or a dictionary to manage a group of related objects. – PM 2Ring Oct 02 '16 at 08:49
  • @PM2Ring I understand the practice of what you mean. Have meaningful names and comments to make it more readable especially when the program gets really big. But the exercise I was working on was: Implement a function that takes as input three variables, and returns the largest of the three. >>> So I thought, hey, I should not repeat myself creating manually var1, var2, and var3. Instead I wanted to use the for loop. Any chance you can tell me how Python allows to automatically create a series of variables? – BitByBit Oct 02 '16 at 08:58
  • 1
    @BitByBit while it is somewhat possible to programmatically create variable names with some contorting of the language, it is hugely frowned upon and not a viable solution. As they have said, just create a list. You can use `list.append()` to add each `input()` to the list, then you can use `max()` or one of the many other [built-in functions](https://docs.python.org/3/library/functions.html) that can work on iterables. Why make three variables (integers), when one will do just fine (a list)? Loop three times, add each `input()` to the list after converting to `int()`, then use `max()`. – charlie Oct 02 '16 at 09:23
  • @Charles I get your point. I was just testing the waters....still learning :-) – BitByBit Oct 02 '16 at 09:52
  • 1
    @BitByBit I get it. It's good to be curious. I've thought about this myself. But lists usually work best in this case. Or in other situations, a dict, which can use `"var1"`, `"var2"`, `"var3"`, etc. as the `keys` to point to some `value`: `{'var1': 1, 'var2': 2, 'var3': 3}` So that's the closest to what you asked for. But in this scenario, a simple list makes the most sense. But you aren't the first to try: http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html – charlie Oct 02 '16 at 10:02

0 Answers0