Im trying to learn python but somehow the command to generate random numbers is not working.
My code is:
import random
##First try
random_integer = random.randint ( a: 1, b: 100 )
This is what i get.
random_integer = random.randint ( a: 1, b: 100 )
^
SyntaxError: invalid syntax
I am using Visual Studios and i installed some plugs already.
In the problems tag i get "a" is not defined.
3
Answers
That isn’t valid python syntax. It seems like you’ve mixed up type hinting and calling a function with keyword arguments. The function you want is defined as
It can be called using unnamed positional parameters as in
where the first parameter is
a
and the second isb
.Or named keyword parameters where the order doesn’t matter
In python if the syntax of a method or function is
function_name(a,b)
, if you are not changing the order(sequence) of the arguments, you can directly call with the arguments without mentioning parameters’ name.If you are not sure about the order or sequence of the parameters, then the parameter name and argument should be separated by an
=
sign.Fixes:
Your code didn’t work as it didn’t use proper syntax.
will generate a random number between a and b inclusive.