# 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: ")
# 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)
Videos
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.
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.
I have tried to declare a variable type in Python with this:
a=int
and to do write the variable I used this:
a=input()
but every time I write in a string like "abcd", it automatically changes the variable type to string and doesn't show any errors. I want to use the "try" and "except" function to show a custom error message when a non-integer variable is assigned to "a".
How can I do this?
If you need to do this, do
isinstance(<var>, int)
unless you are in Python 2.x in which case you want
isinstance(<var>, (int, long))
Do not use type. It is almost never the right answer in Python, since it blocks all the flexibility of polymorphism. For instance, if you subclass int, your new class should register as an int, which type will not do:
class Spam(int): pass
x = Spam(0)
type(x) == int # False
isinstance(x, int) # True
This adheres to Python's strong polymorphism: you should allow any object that behaves like an int, instead of mandating that it be one.
BUT
The classical Python mentality, though, is that it's easier to ask forgiveness than permission. In other words, don't check whether x is an integer; assume that it is and catch the exception results if it isn't:
try:
x += 1
except TypeError:
...
This mentality is slowly being overtaken by the use of abstract base classes, which let you register exactly what properties your object should have (adding? multiplying? doubling?) by making it inherit from a specially-constructed class. That would be the best solution, since it will permit exactly those objects with the necessary and sufficient attributes, but you will have to read the docs on how to use it.
Here's a summary of the different methods mentioned here:
int(x) == xtry x = operator.index(x)isinstance(x, int)isinstance(x, numbers.Integral)
and here's how they apply to a variety of numerical types that have integer value:

You can see they aren't 100% consistent. Fraction and Rational are conceptually the same, but one supplies a .index() method and the other doesn't. Complex types don't like to convert to int even if the real part is integral and imaginary part is 0.
(np.int8|16|32|64(5) means that np.int8(5), np.int32(5), etc. all behave identically)