# declare score as integer
score = int
# declare rating as character
rating = chr
Above two statement, assigns the function int, chr, not declaring the variable with the default value. (BTW, chr is not a type, but a function that convert the code-point value to character)
Do this instead:
score = 0 # or int()
rating = '' # or 'C' # if you want C to be default rating
NOTE score is not need to be initialized, because it's assigned by score = input("Enter score: ")
I’m trying to create a code that takes integers and puts them into a list, I have found a way to do that, however I would like to know how I can deal with if a float is put as an input rather than an integer, I have a very very basic understanding and any help would be appreciated. I can provide anymore information if needed.
Creating a list of integers in Python - Stack Overflow
What does it mean when you assign int to a variable in Python? - Stack Overflow
python - Is a list a variable? - Stack Overflow
List[int] cannot be assigned to a parameter of type List[int | str]
Videos
# declare score as integer
score = int
# declare rating as character
rating = chr
Above two statement, assigns the function int, chr, not declaring the variable with the default value. (BTW, chr is not a type, but a function that convert the code-point value to character)
Do this instead:
score = 0 # or int()
rating = '' # or 'C' # if you want C to be default rating
NOTE score is not need to be initialized, because it's assigned by score = input("Enter score: ")
In python, you can't do static typing (i.e. you can't fix a variable to a type). Python is dynamic typing.
What you need is to force a type to the input variable.
# declare score as integer
score = '0' # the default score
# declare rating as character
rating = 'D' # default rating
# write "Enter score: "
# input score
score = input("Enter score: ")
# here, we are going to force convert score to integer
try:
score = int (score)
except:
print ('score is not convertable to integer')
# if score == 10 Then
# set rating = "A"
# endif
if score == 10:
rating = "A"
print(rating)
Try this:
lst = input('insert numbers: ')
lst = [int(d) for d in lst]
lst
As your comment, try only this one line:
[int(d) for d in input('insert numbers: ')]
I believe you can't perform this operation directly without passing to string type because int type is not iterable. In that case, you can just use input() without int().
You can try this 2 situations to create a list:
In this case, numbers without separation would be placed, such as 1234 (it would be more difficult to get numbers with more than 1 place, for example, 10, 11...)
test1 = input('insert numbers :')
lst = [int(number) for number in test1]
lst
In this way, you perform the separation using comma (',') for the numbers, like 1,12,13,5 and I think this is more appropriate because you can get all numbers.
test2 = input('insert numbers (separate with comma):')
lst = [int(number) for number in test2.split(',')]
lst
Imagine you had a function called func
def func():
print("hello from func")
return 7
If you then assigned func to x you are assigning the function itself to x not the result of the call
x = func # note: no ()
x() # calls func()
y = x() # y is now 7
You're looking at a very similar thing with int in this context.
x = int
y = x('2') # y is now 2
x = int will not make x into an integer. int is the integer type. Doing x = int will set x to the value of the int type. Loosely speaking, x will become an "alias" for the integer type.
If you call the int type on something, like int('2'), it will convert what you give into an integer, if it can. If you assign the result of that call to a variable, it will set that variable to the integer value you got from calling int. So setting x = int('2') will set x to 2.
You should read the Python tutorial to understand how types, variables, and calling work in Python.
No. A list is an object. You assign a list to a name-reference with =.
Thus a = [1,2] produces a which is a name-reference (a pointer essentially) to the underlying list object which you see by looking at globals().
>>> a = [1,2]
>>> globals()
{'a': [1, 2], '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', '__doc__': None}
A list is an instance of a ListType, which is a subclass of an object.
>>> import types
>>> types.ListType.mro()
[<type 'list'>, <type 'object'>]
>>> object
<type 'object'>
>>> b = types.ListType()
>>> b
[]
In Python, the concept of object is quite important (as other users might have pointed out already, I am being slow!).
You can think of list as a list (or actually, an Object) of elements. As a matter of fact, list is a Variable-sized object that represents a collection of items. Python lists are a bit special because you can have mixed types of elements in a list (e.g. strings with int)But at the same time, you can also argue,"What about set, map, tuple, etc.?". As an example,
>>> p = [1,2,3,'four']
>>> p
[1, 2, 3, 'four']
>>> isinstance(p[1], int)
True
>>> isinstance(p[3], str)
True
>>>
In a set, you can vary the size of the set - yes. In that respect, set is a variable that contains unique items - if that satisfies you....
In this way, a map is also a "Variable" sized key-value pair where every unique key has a value mapped to it. Same goes true for dictionary.
If you are curious because of the = sign - you have already used a keyword in your question; "Assignment". In all the high level languages (well most of them anyway), = is the assignment operator where you have a variable name on lhs and a valid value (either a variable of identical type/supertype, or a valid value).