Some difficulties in Python on Begginer level

Question:

Sorry if I’m asking something dumb. I’m begginer btw.
Can I somehow place multiple variables in one variable, like:

login = "user"
enter = input(login + ">")
commandLogin = "login"
commandRegister = "register"
commandExit = "exit"
commandList = commandLogin, commandRegister, commandExit

while enter != commandList:
    print("incorrect command!")
    enter = input(login + ">")
Asked By: rvx

||

Answers:

Well, in the example you’re already doing that.

The main correction is that you probably want while enter not in commandList

Answered By: Jiří Baum

You can use an ‘iterable’, such as a tuple or a list. Check here: Python docs
So for your code you can use:

login = "user"
commandLogin = "login"
commandRegister = "register"
commandExit = "exit"
commandList = [commandLogin, commandRegister, commandExit]

enter = input(login + ">")
while enter not in commandList:
    print("Incorrect command!")
    enter = input(login + ">")
Answered By: David

Sure. You can use a dictionary to put multiple variables in one variable:

>>> a={}
>>> a['first']='this is the first'
>>> a['second']='the second'
>>> a
{'first': 'this is the first', 'second': 'the second'}
>>> a['first']
'this is the first'
>>>

You can also create a class, an object of the class, and access attributes:

>>> class Empty():
...     pass
...
>>> a = Empty()
>>> a.first = 'the first value'
>>> a.second = 'the second value'
>>> a
<__main__.Empty object at 0x7fceb005dbd0>
>>> a.first
'the first value'
>>> a.second
'the second value'
>>> a.third
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Empty' object has no attribute 'third'
>>>

In your example, you probably want to use a list:

>>> a = []
>>> a.append("first")
>>> a.append("second")
>>> a.append("third")
>>> a
['first', 'second', 'third']
>>>

But it’s not really clear what you are trying to do in your example.

Answered By: vy32

The result of this is a tuple. A tuple is similar to a list but once a tuple is created, the values cannot be changed. The technique in the code is called tuple packing and essentially just creates a tuple.

Answered By: gmdev

When you separate multiple variables by comma without any surrounding parenthesis, python creates a tuple for you:

a = 1
b = 2
c = 3

# implicit tuple
x = a, b, c

# equivalent, explicit tuple
x = (a, b, c)

Likewise, you can expand a tuple (or any iterable) into multiple variables:

# Expanding a tuple
x = (1, 2, 3)
a, b, c = x  # a=1, b=2, c=3

# Expanding a list
x = [1, 2, 3]
a, b, c = x  # a=1, b=2, c=3

# Expanding a dict (the keys are expanded into the new variables)
x = {'a': 1, 'b': 2}
a, b = x  # a = 'a', b = 'b'

# Expanding an iterator
a, b = range(2)  # a=0, b=1
Answered By: kwsp

So you can do it by many ways, i’ll give you two of them!

You can always try to get to know some other 😉

You can do a list(changeable list) like this : colors = ["white", "blue", "red"].

Or you can do a tuple ( non-changeable list ( you can change the variable, but not the list itself )) like this : colors = ("white", "blue", "red")

Answered By: Tomas Pereira
Answered By: Aditya Parihar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.